package com.shop2cn.iapi.shenqisettlement.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.ymttest.business.service.shenqi.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.BriefMemberInfoResp;
import com.shop2cn.iapi.shenqisettlement.parameter.OnHpSettledReq;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.DistributorCommisionRateProductBean;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.DistributorProductBean;
import com.shop2cn.iapi.shenqisettlement.service.DGSQBaseService;
import com.shop2cn.iapi.shenqisettlement.service.OnHpSettledCall;
import com.shop2cn.iapi.shenqisettlement.verify.ShenqiSettlementBuildVerifyService;
import com.shop2cn.iapi.sqdistributor.parameter.request.pojo.DistributionAgencyLevelCommissionConfigVo;
import com.shop2cn.iapi.sqoptrade.parameter.enums.CancelOrderTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.godmch.parameter.MerchantFundCollectConfigResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.shenqisettlement.DistributorProductDetail;
import com.ymttest.database.model.shenqisettlement.DistributorSettlement;
import com.ymttest.database.model.shenqisettlement.DistributorSettlementDetail;
import com.ymttest.database.model.shenqisettlement.OrderSettlement;
import com.ymttest.database.model.shenqisettlement.OrderSettlementDetail;
import com.ymttest.database.model.sqdistributor.CommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencyCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencyConfig;
import com.ymttest.database.model.sqdistributor.DistributionAgencyProductCategoryCommissionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorConfig;
import com.ymttest.database.model.sqdistributor.DistributionConfig;
import com.ymttest.database.model.sqdistributor.DistributorConsumerRelation;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.model.sqdistributor.PartnerConfig;
import com.ymttest.database.model.sqdistributor.PartnerRewardRate;
import com.ymttest.database.model.sqdistributor.ProductCommissionConfig;
import com.ymttest.database.model.sqdistributor.ProductCommissionRate;
import com.ymttest.database.model.sqdistributor.UserCpsEvent;
import com.ymttest.database.model.sqrefund.RefundBill;
import com.ymttest.database.model.sqrefund.RefundProductDetail;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentDetailPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.database.sqlwapper.shenqisettlement.DistributorProductDetailWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.DistributorSettlementDetailWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.DistributorSettlementWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.OrderSettlementDetailWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.OrderSettlementWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.CommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyProductCategoryCommissionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorConsumerRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.PartnerConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.PartnerRewardRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.ProductCommissionConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.ProductCommissionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.UserCpsEventWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundProductDetailWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_ShenqiDistributorSettlement {

	private static OnHpSettledReq hpSettledReq = null;
	private static OnHpSettledCall hpSettledCall = new OnHpSettledCall();

	// 盛付通手续费率
	private static String shengPayRate = "0.001";

	private static OrderShardingWapper wapper = new OrderShardingWapper();

	private static RefundBillWrapper refundBillWrapper = new RefundBillWrapper();
	private static RefundProductDetailWrapper refundProductDetailWrapper = new RefundProductDetailWrapper();

	private static OrderSettlementWrapper orderSettlementWrapper = new OrderSettlementWrapper();
	private static OrderSettlementDetailWrapper orderSettlementDetailWrapper = new OrderSettlementDetailWrapper();

	private static DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();
	private static DistributorConsumerRelationWrapper distributorConsumerRelationWrapper = new DistributorConsumerRelationWrapper();
	private static DistributionConfigWrapper distributorConfigWrapper = new DistributionConfigWrapper();
	private static DistributionAgencyConfigWrapper distributionAgencyConfigWrapper = new DistributionAgencyConfigWrapper();
	private static CommisionRateWrapper commisionRateWrapper = new CommisionRateWrapper();
	private static DistributionAgencyCommisionRateWrapper distributionAgencyCommisionRateWrapper = new DistributionAgencyCommisionRateWrapper();
	private static UserCpsEventWrapper userCpsEventWrapper = new UserCpsEventWrapper();
	private static DistributionAgencySubDistributorConfigWrapper distributionAgencySubDistributorConfigWrapper = new DistributionAgencySubDistributorConfigWrapper();
	private static DistributionAgencySubDistributorCommisionRateWrapper distributionAgencySubDistributorCommisionRateWrapper = new DistributionAgencySubDistributorCommisionRateWrapper();
	private static DistributionAgencyProductCategoryCommissionRateWrapper distributionAgencyProductCategoryCommissionRateWrapper = new DistributionAgencyProductCategoryCommissionRateWrapper();

	private static PartnerConfigWrapper partnerConfigWrapper = new PartnerConfigWrapper();
	private static PartnerRewardRateWrapper partnerRewardRateWrapper = new PartnerRewardRateWrapper();

	private static ProductCommissionConfigWrapper productCommissionConfigWrapper = new ProductCommissionConfigWrapper();
	private static ProductCommissionRateWrapper productCommissionRateWrapper = new ProductCommissionRateWrapper();

	private static DistributorSettlementWrapper distributorSettlementWrapper = new DistributorSettlementWrapper();
	private static DistributorSettlementDetailWrapper distributorSettlementDetailWrapper = new DistributorSettlementDetailWrapper();
	private static DistributorProductDetailWrapper distributorProductDetailWrapper = new DistributorProductDetailWrapper();

	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;
	private static long chainStoreGoodsOwnerId = ShenqiUserSet.chainStoreGoodsOwnerId;

	private static long fundNotCollectGoodsOwnerId = ShenqiUserSet.fundNotCollectGoodsOwnerId;
	private static long macaoCDFGoodsOwnerId = ShenqiUserSet.macaoCDFGoodsOwnerId;

	private static long platformSupplierGoodsOwnerId = ShenqiUserSet.platformSupplierGoodsOwnerId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代购神器分销结算");

		/**
		 * 将未结算的结算单置为废弃,防止结算的时间过长
		 */
		orderSettlementWrapper.updateStatus();
	}

	@Before
	public void caseUp() {
		String accountid1;
		try {
			accountid1 = AccountCallServiceV2.getAccountId(20238699);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					9000000), new BigDecimal(0));

			hpSettledReq = new OnHpSettledReq();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("api/ShenqiDistributorSettlement");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void onHpSettled(long paymentId, PaymentPo payment,
			String shengPayRate, int count) throws Exception {

		if (count < 6) {

			hpSettledReq.setTradingId(paymentId);
			hpSettledReq.setOrderAmount(payment.getRealPaidAmount());
			hpSettledReq.setSettleCurrency(payment.getSettleCurrency());
			hpSettledReq
					.setSettleAmount(payment.getRealForeignAmount() != null ? payment
							.getRealForeignAmount() : payment
							.getForeignAmount());

			if (shengPayRate != null) {

				hpSettledReq.setSettleFee(payment.getRealPaidAmount().multiply(
						new BigDecimal(shengPayRate)));
			}

			hpSettledCall.setData(hpSettledReq);
			hpSettledCall.callService();

			if (!"ok".equalsIgnoreCase(hpSettledCall.getReturnData())) {

				Thread.sleep(3000 + count * 2000);
				count++;
				this.onHpSettled(paymentId, payment, shengPayRate, count);
			}
		} else {

			throw new Exception();
		}
	}

	public int getSettlementStatus(Boolean isCanceled, Boolean settled, Integer delayDays,
                                   List<RefundBill> refundBillPoList, Long orderId, OrderSettlement orderSettlement,
                                   BigDecimal commissionAmount, BigDecimal bonusAmount) {

        if (orderSettlement.getSettlement_status() == 3)
            return 0;

        if (orderSettlement.getSettlement_status() == 1 && commissionAmount.compareTo(BigDecimal.ZERO) == 0
                && Utils.zeroIfNull(bonusAmount).compareTo(BigDecimal.ZERO) == 0)
            return 2;

		if (null != refundBillPoList) {

			if (null != refundBillPoList
					&& !RefundService.isEntireRefunded(orderId) && settled
					&& delayDays == 0)
				return 1;
			else if (null != refundBillPoList
					&& !RefundService.isEntireRefunded(orderId))
				return 0;
			else
				return 4;
		} else {

			if (isCanceled && !settled)
				return 4;
			else if (settled && delayDays == 0)
				return 1;
			else
				return 0;
		}
	}

	/**
	 * 是否为子分销员
	 *
	 * @param distributorType
	 * @return
	 */
	private Boolean isSubDistributor(int distributorType) {

		if (distributorType == 1001 || distributorType == 2001)
			return true;
		else
			return false;
	}

	/**
	 * 判断DistributorRelation隶属于分销机构还是CPS
	 *
	 * @param distributorRelation
	 * @return
	 */
	private int getDistributorType(DistributorRelation distributorRelation) {

		if (null == distributorRelation)
			return -1;
		else if (distributorRelation.getDistributor_type() == 0)
			return 0;
		else if (distributorRelation.getDistributor_type() == 1
				|| distributorRelation.getDistributor_type() == 1001)
			return 1;
		else if (distributorRelation.getDistributor_type() == 2
				|| distributorRelation.getDistributor_type() == 2001)
			return 2;
		else
			return 3;
	}

	private int calDelayDays(DistributionAgencyConfig distributionAgencyConfig) {

		int delayDays = 0;

		// 如果是周期结算Distribution_settlement_delay_days为下个月的日期
		if (Utils.zeroIfNull(distributionAgencyConfig
				.getSettlement_periodicity_type()) == 1) {

			Calendar cal = Calendar.getInstance();

			cal.add(Calendar.MONTH, 1);
			cal.set(Calendar.DATE, distributionAgencyConfig
					.getDistribution_settlement_delay_days());
			cal.set(Calendar.HOUR_OF_DAY, 1);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);

			delayDays = Utils.daysBetween(YMTDateUtil.getDate(), cal.getTime());
		} else
			delayDays = distributionAgencyConfig
					.getDistribution_settlement_delay_days();

		return delayDays;
	}

	public DistributorCommisionRateProductBean buildDistributorCommisionRateProductBean(
			DistributorRelation relation, Long id, int index,
			ArrayList<DistributorProductBean> distributorProductBeanList,
			PaymentPo paymentPo, OrderSettlement orderSettlement, List<RefundBill> refundBillPoList) {

		DistributorCommisionRateProductBean bean = new DistributorCommisionRateProductBean();

		Long merchantId = relation.getMerchant_id();
		Integer levelIndex = relation.getDistributor_level_index();

		if (Utils.zeroIfNull(relation.getDistributor_type()) == 0) {

			// 普通分销员
			CommisionRate commisionRate = commisionRateWrapper
					.selectByConfigIdAndLevel(id, levelIndex);

			bean.setDistributorRelation(relation);
			bean.setCommisionRate(commisionRate);

			if (index == 0)
				bean.setCurrencyRate(commisionRate.getLayer1_rate());
			else if (index == 1)
				bean.setCurrencyRate(commisionRate.getLayer2_rate());
			else
				bean.setCurrencyRate(commisionRate.getLayer3_rate());

		} else if (Utils.zeroIfNull(relation.getDistributor_type()) == 1) {

			// 分销机构
			DistributionAgencyCommisionRate distributionAgencyCommisionRate = distributionAgencyCommisionRateWrapper
					.selectByConfigId(id);

			bean.setDistributorRelation(relation);
			bean.setDistributionAgencyCommisionRate(distributionAgencyCommisionRate);
			bean.setCurrencyRate(distributionAgencyCommisionRate
					.getLayer1_rate());
		} else if (Utils.zeroIfNull(relation.getDistributor_type()) == 2) {

			// CPS
			DistributionAgencyCommisionRate distributionAgencyCommisionRate = distributionAgencyCommisionRateWrapper
					.selectByConfigId(id);

			bean.setDistributorRelation(relation);
			bean.setDistributionAgencyCommisionRate(distributionAgencyCommisionRate);
			bean.setCurrencyRate(distributionAgencyCommisionRate
					.getLayer1_rate());

		} else if (Utils.zeroIfNull(relation.getDistributor_type()) == 1001) {

			// 分销机构对子分销员配置
			DistributionAgencySubDistributorConfig distributionAgencySubDistributorConfig = distributionAgencySubDistributorConfigWrapper
					.selectByMerchantIdAndDistributorIdAndType(merchantId,
							relation.getParent_distributor_id(), 1);

			if (null == distributionAgencySubDistributorConfig) {

				DistributionAgencySubDistributorCommisionRate distributionAgencySubDistributorCommisionRate = new DistributionAgencySubDistributorCommisionRate();

				distributionAgencySubDistributorCommisionRate
						.setMerchant_id(merchantId);
				distributionAgencySubDistributorCommisionRate
						.setLayer1_rate(BigDecimal.ZERO);
				distributionAgencySubDistributorCommisionRate
						.setDistributor_level_label("普通分销员");
				distributionAgencySubDistributorCommisionRate
						.setDistributor_level_index(0);
				distributionAgencySubDistributorCommisionRate
						.setDistribution_agency_sub_distributor_config_id(0L);

				bean.setDistributorRelation(relation);
				bean.setDistributionAgencySubDistributorCommisionRate(distributionAgencySubDistributorCommisionRate);
				bean.setCurrencyRate(distributionAgencySubDistributorCommisionRate
						.getLayer1_rate());
			} else {

				Long configId = distributionAgencySubDistributorConfig.getId();

				DistributionAgencySubDistributorCommisionRate distributionAgencySubDistributorCommisionRate = distributionAgencySubDistributorCommisionRateWrapper
						.selectByConfigIdAndLevelIndex(configId, levelIndex);

				if (null == distributionAgencySubDistributorCommisionRate) {

					distributionAgencySubDistributorCommisionRate = new DistributionAgencySubDistributorCommisionRate();

					distributionAgencySubDistributorCommisionRate
							.setMerchant_id(merchantId);
					distributionAgencySubDistributorCommisionRate
							.setLayer1_rate(BigDecimal.ZERO);
					distributionAgencySubDistributorCommisionRate
							.setDistributor_level_label("普通分销员");
					distributionAgencySubDistributorCommisionRate
							.setDistributor_level_index(0);
					distributionAgencySubDistributorCommisionRate
							.setDistribution_agency_sub_distributor_config_id(0L);
				}

				bean.setDistributorRelation(relation);
				bean.setDistributionAgencySubDistributorCommisionRate(distributionAgencySubDistributorCommisionRate);
				bean.setCurrencyRate(distributionAgencySubDistributorCommisionRate
						.getLayer1_rate());
			}
		} else {

			// CPS对子分销员配置
			DistributionAgencySubDistributorConfig distributionAgencySubDistributorConfig = distributionAgencySubDistributorConfigWrapper
					.selectByMerchantIdAndDistributorIdAndType(merchantId,
							relation.getParent_distributor_id(), 2);

			if (null == distributionAgencySubDistributorConfig) {

				DistributionAgencySubDistributorCommisionRate distributionAgencySubDistributorCommisionRate = new DistributionAgencySubDistributorCommisionRate();

				distributionAgencySubDistributorCommisionRate
						.setMerchant_id(merchantId);
				distributionAgencySubDistributorCommisionRate
						.setLayer1_rate(BigDecimal.ZERO);
				distributionAgencySubDistributorCommisionRate
						.setDistributor_level_label("普通分销员");
				distributionAgencySubDistributorCommisionRate
						.setDistributor_level_index(0);
				distributionAgencySubDistributorCommisionRate
						.setDistribution_agency_sub_distributor_config_id(0L);

				bean.setDistributorRelation(relation);
				bean.setDistributionAgencySubDistributorCommisionRate(distributionAgencySubDistributorCommisionRate);
				bean.setCurrencyRate(distributionAgencySubDistributorCommisionRate
						.getLayer1_rate());
			} else {

				Long configId = distributionAgencySubDistributorConfig.getId();

				DistributionAgencySubDistributorCommisionRate distributionAgencySubDistributorCommisionRate = distributionAgencySubDistributorCommisionRateWrapper
						.selectByConfigIdAndLevelIndex(configId, levelIndex);

				if (null == distributionAgencySubDistributorCommisionRate) {

					distributionAgencySubDistributorCommisionRate = new DistributionAgencySubDistributorCommisionRate();

					distributionAgencySubDistributorCommisionRate
							.setMerchant_id(merchantId);
					distributionAgencySubDistributorCommisionRate
							.setLayer1_rate(BigDecimal.ZERO);
					distributionAgencySubDistributorCommisionRate
							.setDistributor_level_label("普通分销员");
					distributionAgencySubDistributorCommisionRate
							.setDistributor_level_index(0);
					distributionAgencySubDistributorCommisionRate
							.setDistribution_agency_sub_distributor_config_id(0L);
				}

				bean.setDistributorRelation(relation);
				bean.setDistributionAgencySubDistributorCommisionRate(distributionAgencySubDistributorCommisionRate);
				bean.setCurrencyRate(distributionAgencySubDistributorCommisionRate
						.getLayer1_rate());
			}
		}

		BigDecimal distributorBidCurrencySettleAmount = BigDecimal.ZERO;
		BigDecimal distributorSettlementCurrencySettleAmount = BigDecimal.ZERO;

		BigDecimal foreignVsBidExchangeRate = null;
		BigDecimal bidVsSettlemntExchangeRate = new BigDecimal(
				paymentPo.getExchangeRate() == null ? "1.00"
						: paymentPo.getExchangeRate());

		if (orderSettlement.getBid_currency().equalsIgnoreCase("CNY")
				|| !orderSettlement.getBid_currency().equalsIgnoreCase(
						orderSettlement.getCurrency())) {
			// 如果商品标价币种为人民币或货头的结算币种与标价币种不一致
			foreignVsBidExchangeRate = new BigDecimal(
					orderSettlement.getExchange_rate());
		} else {
			foreignVsBidExchangeRate = paymentPo.getShenqiPaymentRmbAmount()
					.multiply(
							BigDecimal.ONE.divide(
									paymentPo.getRealForeignAmount(), 7,
									BigDecimal.ROUND_DOWN));
		}

		HashMap<String, BigDecimal> refundBillNoRemainsSettleAmountMap = new HashMap<>();

		// 分销机构/CPS的子分销员没有商品分销信息
		if (null != distributorProductBeanList) {
			for (DistributorProductBean distributorProductBean : distributorProductBeanList) {

				BigDecimal currencyRate = BigDecimal.ZERO;
				OrderDetailPo orderDetailPo = distributorProductBean
						.getOrderDetailPo();

				// 如果是分销机构，存在指定类目分佣，则使用类目分佣，否则使用平台分佣
				// 如果是普通分销员，存在指定商品分佣，则使用商品分佣，否则使用平台分佣
				// 如果是CPS，只使用平台分佣
				if (relation.getDistributor_type() == 1) {

					long firstCategoryId = orderDetailPo.getFirstCategoryId()
							.longValue();
					long secondCategoryId = orderDetailPo.getSecondCategoryId()
							.longValue();

					DistributionAgencyProductCategoryCommissionRate distributionAgencyProductCategoryCommissionRate = distributionAgencyProductCategoryCommissionRateWrapper
							.selectByMerchangIdAndCategoryId(merchantId,
									firstCategoryId, secondCategoryId);

					if (null == distributionAgencyProductCategoryCommissionRate) {

						Logger.comment(String.format(
								"指定一级类目%s，二级类目%s，没有分佣，使用平台分佣", firstCategoryId,
								secondCategoryId));

						distributorProductBean.setProductConfigId(null);
						currencyRate = bean.getCurrencyRate();

					} else {

						distributorProductBean
								.setProductCommissionRate(new ProductCommissionRate());

						currencyRate = distributionAgencyProductCategoryCommissionRate
								.getCommission_rate();
						distributorProductBean
								.setProductCurrencyRate(currencyRate);
						distributorProductBean
								.setProductConfigId(distributionAgencyProductCategoryCommissionRate
										.getId());
					}
				} else if (relation.getDistributor_type() == 0) {

					String productId = orderDetailPo.getProductId();

					ProductCommissionConfig productCommissionConfig = productCommissionConfigWrapper
							.selectByMerchantIdAndProductId(merchantId,
									orderDetailPo.getProductId());

					if (null == productCommissionConfig) {

						Logger.comment(String.format("指定商品%s，没有分佣，使用平台分佣",
								productId));

						distributorProductBean.setProductConfigId(null);
						currencyRate = bean.getCurrencyRate();

					} else {

						ProductCommissionRate productCommissionRate = productCommissionRateWrapper
								.selectByConfigIdAndTypeAndLevel(
										productCommissionConfig.getId(), 1,
										levelIndex);

						if (null == productCommissionRate) {

							Logger.comment(String.format(
									"指定商品%s，没有分销员分佣，使用平台分佣", productId));

							distributorProductBean.setProductConfigId(null);
							currencyRate = bean.getCurrencyRate();
						} else {

							distributorProductBean
									.setProductCommissionRate(productCommissionRate);

							if (index == 0)
								distributorProductBean
										.setProductCurrencyRate(productCommissionRate
												.getLayer1_rate());
							else if (index == 1)
								distributorProductBean
										.setProductCurrencyRate(productCommissionRate
												.getLayer2_rate());
							else
								distributorProductBean
										.setProductCurrencyRate(productCommissionRate
												.getLayer3_rate());

							currencyRate = distributorProductBean
									.getProductCurrencyRate();
							distributorProductBean
									.setProductConfigId(productCommissionConfig
											.getId());
						}
					}
				} else {

					distributorProductBean.setProductConfigId(0L);
					currencyRate = bean.getCurrencyRate();
				}

				distributorProductBean = buildDistributorProductBean(
						distributorProductBean, orderDetailPo, currencyRate,
						bidVsSettlemntExchangeRate);

				distributorBidCurrencySettleAmount = distributorBidCurrencySettleAmount
						.add(distributorProductBean
								.getBidCurrencySettleAmount());
				distributorSettlementCurrencySettleAmount = distributorSettlementCurrencySettleAmount
						.add(distributorProductBean
								.getSettlementCurrencySettleAmount());
			}

			BigDecimal totalPayableAmount = distributorProductBeanList
					.stream()
					.map(distributorProductBean -> distributorProductBean
							.getProductPayableAmount())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			// 退款单处理
			if (null == refundBillPoList || refundBillPoList.size() == 0) {

				distributorProductBeanList
						.stream()
						.forEach(
								distributorProductBean -> {
									distributorProductBean
											.setRefundedProductBidAmount(BigDecimal.ZERO);
									distributorProductBean
											.setRefundedProductSettleAmount(BigDecimal.ZERO);
								});
			} else {

				HashMap<String, BigDecimal> catalogIdRefundedSettleAmountMap = new HashMap<>();
				BigDecimal remainsBidCurrencySettleAmount = distributorBidCurrencySettleAmount;

				for (RefundBill refundBillPo : refundBillPoList) {

					String refundBillNo = refundBillPo.getRefund_no();

					int n = 0;

					refundBillNoRemainsSettleAmountMap.put(refundBillNo, remainsBidCurrencySettleAmount);

					for (DistributorProductBean distributorProductBean : distributorProductBeanList) {

						OrderDetailPo orderDetailPo = distributorProductBean
								.getOrderDetailPo();
						String catalogId = orderDetailPo.getCatalogId();

						BigDecimal catalogIdRefundedSettleAmount = Utils
								.zeroIfNull(catalogIdRefundedSettleAmountMap
										.get(catalogId));

						BigDecimal bidCurrencySettleAmount = distributorProductBean
								.getBidCurrencySettleAmount().subtract(
										catalogIdRefundedSettleAmount);

						List<RefundProductDetail> refundProductDetails = refundProductDetailWrapper
								.selectByRefundBillNoAndCatalogId(refundBillNo,
										orderDetailPo.getCatalogId());

						if (null == refundProductDetails
								|| refundProductDetails.size() == 0) {

							BigDecimal totalSettleRefundSettleAmount = catalogIdRefundedSettleAmount
									.multiply(bidVsSettlemntExchangeRate).setScale(
											2, BigDecimal.ROUND_DOWN);

							HashMap<String, BigDecimal> refundBillNoRefundedSettleAmountMap = null;

							if (null == distributorProductBean
									.getRefundedSettleAmountMap()) {

								refundBillNoRefundedSettleAmountMap = new HashMap<>();
								refundBillNoRefundedSettleAmountMap.put(
										refundBillNo, BigDecimal.ZERO);
							} else {

								refundBillNoRefundedSettleAmountMap = distributorProductBean
										.getRefundedSettleAmountMap();

								refundBillNoRefundedSettleAmountMap.put(
										refundBillNo, BigDecimal.ZERO);
							}

							distributorProductBean
									.setRefundedProductBidAmount(catalogIdRefundedSettleAmount);
							distributorProductBean
									.setRefundedProductSettleAmount(totalSettleRefundSettleAmount);
							distributorProductBean
									.setRefundedSettleAmountMap(refundBillNoRefundedSettleAmountMap);
						} else {

							RefundProductDetail refundProductDetail = refundProductDetails
									.get(0);

							if (refundBillPo.getRefund_type() == 1) {

								BigDecimal leftBidSettleAmount = refundBillPo
										.getTotal_refund_product_amount()
										.divide(totalPayableAmount, 6,
												BigDecimal.ROUND_DOWN)
										.multiply(remainsBidCurrencySettleAmount)
										.setScale(2, BigDecimal.ROUND_HALF_UP);

								catalogIdRefundedSettleAmount = leftBidSettleAmount
										.multiply(
												bidCurrencySettleAmount
														.divide(remainsBidCurrencySettleAmount,
																6,
																BigDecimal.ROUND_DOWN))
										.setScale(2, BigDecimal.ROUND_DOWN)
										.add(catalogIdRefundedSettleAmount);
							} else {

								catalogIdRefundedSettleAmount = bidCurrencySettleAmount
										.multiply(
												new BigDecimal(
														refundProductDetail
																.getRefund_num()))
										.divide(new BigDecimal(
												refundProductDetail
														.getProduct_num()), 2,
												BigDecimal.ROUND_DOWN)
										.add(catalogIdRefundedSettleAmount);
							}

							HashMap<String, BigDecimal> refundBillNoRefundedSettleAmountMap = null;

							if (null == distributorProductBean
									.getRefundedSettleAmountMap()) {

								refundBillNoRefundedSettleAmountMap = new HashMap<>();

								refundBillNoRefundedSettleAmountMap.put(
										refundBillNo, catalogIdRefundedSettleAmount);
							} else {

								refundBillNoRefundedSettleAmountMap = distributorProductBean
										.getRefundedSettleAmountMap();

								refundBillNoRefundedSettleAmountMap.put(
										refundBillNo, catalogIdRefundedSettleAmount);
							}

							catalogIdRefundedSettleAmountMap.put(catalogId,
									catalogIdRefundedSettleAmount);

							BigDecimal totalSettleRefundSettleAmount = catalogIdRefundedSettleAmount
									.multiply(bidVsSettlemntExchangeRate).setScale(
											2, BigDecimal.ROUND_DOWN);

							distributorProductBean
									.setRefundedProductBidAmount(catalogIdRefundedSettleAmount);
							distributorProductBean
									.setRefundedProductSettleAmount(totalSettleRefundSettleAmount);
							distributorProductBean
									.setRefundedSettleAmountMap(refundBillNoRefundedSettleAmountMap);
						}

						remainsBidCurrencySettleAmount = remainsBidCurrencySettleAmount
								.subtract(catalogIdRefundedSettleAmount);
					}

					n = n + 1;
				}
			}
		}

		String foreignCurrency = orderSettlement.getCurrency();

		bean.setDistributorProductBeanList(distributorProductBeanList);
		bean.setDistributorBidCurrencySettleAmount(distributorBidCurrencySettleAmount);
		bean.setDistributorSettlementCurrencySettleAmount(distributorSettlementCurrencySettleAmount);
		bean.setBidVsSettlemntExchangeRate(bidVsSettlemntExchangeRate);
		bean.setForeignVsBidExchangeRate(foreignVsBidExchangeRate);
		bean.setPayChannelType(orderSettlement.getPay_channel_type());
		bean.setForeignCurrency(foreignCurrency);
		bean.setRefundBillNoRemainsSettleAmountMap(refundBillNoRemainsSettleAmountMap);

		return bean;
	}

	/**
	 * 获取分销链，如果下单时传入了客服，则没有分销数据 CPS->分销机构->分销员
	 *
	 * @param sellerId
	 * @param userId
	 * @param orderDetailPoList
	 * @param paymentPo
	 * @param orderSettlement
	 * @param csId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DistributorCommisionRateProductBean> buildDistributorChain(
			Long sellerId, Long userId, List<OrderDetailPo> orderDetailPoList,
			PaymentPo paymentPo, OrderSettlement orderSettlement, Long csId,
			List<RefundBill> refundBillPoList) throws Exception {

		csId = 0L;

		Long masterShopId = SqDistributorService.getMasterShopId(sellerId);

		if (masterShopId != 0L)
			sellerId = SqDistributorService.getMasterShopId(sellerId);

		boolean isInnerSale = false;

		for (OrderDetailPo detailPo : orderDetailPoList) {

			int activityType = SqActivityService.getActivityList(sellerId,
					detailPo.getActivityId());

			isInnerSale = Utils.zeroIfNull(activityType) == 22 ? true : false;

			// 只要有一个商品为内购，订单就没有分销数据
			if (isInnerSale)
				break;
		}

		if (isInnerSale) {

			Logger.comment("内购订单，没有分销数据");
			return null;
		}

		ArrayList<DistributorCommisionRateProductBean> chain = new ArrayList<>();

		DistributorConsumerRelation distributorConsumerRelation = distributorConsumerRelationWrapper
				.selectByMerchantIdAndUserId(sellerId, userId);
		DistributorRelation distributorRelation = null;

		BriefMemberInfoResp briefMemberInfoResp = SqDistributorService
				.getBriefMemberInfo(sellerId, userId);

		Integer dsId = briefMemberInfoResp.getDsId();

		// 商户分销员配置
		DistributionConfig distributionConfig = distributorConfigWrapper
				.selectByMerchantId(sellerId);

		// CPS机构配置
		DistributionAgencyConfig distributionCPSConfig = distributionAgencyConfigWrapper
				.selectByMerchantIdAndAgencyType(sellerId, 2);

		// 商户分销员机构配置
		DistributionAgencyConfig distributionAgencyConfig = distributionAgencyConfigWrapper
				.selectByMerchantIdAndAgencyType(sellerId, 1);

		// 如果是自购
		if (dsId > 0) {
			distributorRelation = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(sellerId,
							(long) briefMemberInfoResp.getDsId());
		} else {

			UserCpsEvent userCpsEvent = userCpsEventWrapper
					.select24HEarlistByUserId(userId, YMTDateUtil
							.getBeforeOrNextHour(
									-SqDistributorService.cpsExpirationTime,
									YMTDateUtil.YMDHMSS));

			if (userCpsEvent == null) {

				Logger.comment(String.format(
						"%s小时内，用户没有点击任何CPS分享的链接，没有CPS分销数据",
						SqDistributorService.cpsExpirationTime));

				if (null == distributorConsumerRelation) {

					Logger.comment("买家没有和商户绑定，无法生成分销数据");
					return null;
				}

				if (distributorConsumerRelation.getExpiration_date() != null
						&& YMTDateUtil.diffSecond(distributorConsumerRelation
								.getExpiration_date(), YMTDateUtil.getDate()) > 0) {
					Logger.comment("用户与分销员关系过期，无法分佣");
					return null;
				}

				distributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(sellerId,
								distributorConsumerRelation.getDistributor_id());
			} else {

				distributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(sellerId,
								userCpsEvent.getDistributor_id());
			}
		}

		ArrayList<DistributorRelation> distributorRelationList = new ArrayList<>();

		// 如果商户没有分销员、分销员机构、CPS配置，则返回为null
		if (null == distributionCPSConfig && null == distributionConfig
				&& null == distributionAgencyConfig)
			return null;

		if (distributionCPSConfig != null
				&& this.getDistributorType(distributorRelation) == 2) {

			Logger.comment("CPS分佣链条");

			DistributorRelation parentDistributorRelation = null;
			DistributorCommisionRateProductBean subDistributorCommisionRateProductBean = null;

			// 记下引流的id和name
			Long originDistributorId = distributorRelation.getDistributor_id();
			String originDistributorName = distributorRelation
					.getDistributor_name();

			// 如果邀请人是CPS的子分销员
			if (distributorRelation.getDistributor_type() == 2001) {

				subDistributorCommisionRateProductBean = this
						.buildDistributorCommisionRateProductBean(
								distributorRelation,
								distributionCPSConfig.getId(), 0, null,
								paymentPo, orderSettlement, refundBillPoList);

				subDistributorCommisionRateProductBean
						.setOriginDistributorId(originDistributorId);
				subDistributorCommisionRateProductBean
						.setOriginDistributorName(originDistributorName);

				chain.add(subDistributorCommisionRateProductBean);

				// 查询上级的CPS
				parentDistributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(sellerId,
								distributorRelation.getParent_distributor_id());
			}

			ArrayList<DistributorProductBean> distributorProductBeanList = new ArrayList<>();

			for (OrderDetailPo detailPo : orderDetailPoList) {

				DistributorProductBean distributorProductBean = new DistributorProductBean();
				distributorProductBean.setOrderDetailPo(detailPo);

				distributorProductBeanList.add(distributorProductBean);
			}

			DistributorCommisionRateProductBean distributorCommisionRateProductBean = this
					.buildDistributorCommisionRateProductBean(
							parentDistributorRelation == null ? distributorRelation
									: parentDistributorRelation,
							distributionCPSConfig.getId(), 0,
							distributorProductBeanList, paymentPo,
							orderSettlement, refundBillPoList);

			distributorCommisionRateProductBean
					.setOriginDistributorId(originDistributorId);
			distributorCommisionRateProductBean
					.setOriginDistributorName(originDistributorName);

			// 如果列表里已经有子分销员信息，则更新子分销员的结算金额和商品信息
			if (chain.size() > 0) {

				BigDecimal subDistributorRate = subDistributorCommisionRateProductBean
						.getDistributionAgencySubDistributorCommisionRate()
						.getLayer1_rate();
				BigDecimal distributorSettleAmount = distributorCommisionRateProductBean
						.getDistributorSettlementCurrencySettleAmount();

				subDistributorCommisionRateProductBean
						.setDistributorSettlementCurrencySettleAmount(distributorSettleAmount
								.multiply(subDistributorRate).setScale(2,
										BigDecimal.ROUND_DOWN));
				subDistributorCommisionRateProductBean
						.setDistributorProductBeanList(distributorProductBeanList);
			}

			chain.add(distributorCommisionRateProductBean);

			int delayDays = this.calDelayDays(distributionCPSConfig);

			chain.stream().forEach(
					bean -> {
						bean.setSettlementDelayDays(delayDays);
						bean.setSettlementWay(distributionCPSConfig
								.getSettlement_way());
					});

		} else if (distributionAgencyConfig != null
				&& this.getDistributorType(distributorRelation) == 1) {

			if (csId > 0) {
				Logger.comment("用户已经绑定了客服，分销机构没有任何分销数据");
				return null;
			}

			Logger.comment("分销机构分佣链条");

			DistributorRelation parentDistributorRelation = null;
			DistributorCommisionRateProductBean subDistributorCommisionRateProductBean = null;

			// 记下引流的id和name
			Long originDistributorId = distributorRelation.getDistributor_id();
			String originDistributorName = distributorRelation
					.getDistributor_name();

			// 如果邀请人是分销机构的子分销员
			if (distributorRelation.getDistributor_type() == 1001) {

				if (distributorRelation.getDistributor_status() != 1) {

					subDistributorCommisionRateProductBean = this
							.buildDistributorCommisionRateProductBean(
									distributorRelation,
									distributionAgencyConfig.getId(), 0, null,
									paymentPo, orderSettlement, refundBillPoList);

					subDistributorCommisionRateProductBean
							.setOriginDistributorId(originDistributorId);
					subDistributorCommisionRateProductBean
							.setOriginDistributorName(originDistributorName);

					chain.add(subDistributorCommisionRateProductBean);

					// 查询上级的分销机构
					parentDistributorRelation = distributorRelationWrapper
							.selectByMerchantIdAndDistributorId(sellerId,
									distributorRelation
											.getParent_distributor_id());
				} else {

					// 查询上级的分销机构
					parentDistributorRelation = distributorRelationWrapper
							.selectByMerchantIdAndDistributorId(sellerId,
									distributorRelation
											.getParent_distributor_id());
					originDistributorId = parentDistributorRelation
							.getDistributor_id();
					originDistributorName = parentDistributorRelation
							.getDistributor_name();
				}
			}

			ArrayList<DistributorProductBean> distributorProductBeanList = new ArrayList<>();

			for (OrderDetailPo detailPo : orderDetailPoList) {

				DistributorProductBean distributorProductBean = new DistributorProductBean();
				distributorProductBean.setOrderDetailPo(detailPo);

				distributorProductBeanList.add(distributorProductBean);
			}

			DistributorCommisionRateProductBean distributorCommisionRateProductBean = this
					.buildDistributorCommisionRateProductBean(
							parentDistributorRelation == null ? distributorRelation
									: parentDistributorRelation,
							distributionAgencyConfig.getId(), 0,
							distributorProductBeanList, paymentPo,
							orderSettlement, refundBillPoList);

			distributorCommisionRateProductBean
					.setOriginDistributorId(originDistributorId);
			distributorCommisionRateProductBean
					.setOriginDistributorName(originDistributorName);

			// 如果列表里已经有子分销员信息，则更新子分销员的结算金额和商品信息
			if (chain.size() > 0) {

				BigDecimal subDistributorRate = subDistributorCommisionRateProductBean
						.getDistributionAgencySubDistributorCommisionRate()
						.getLayer1_rate();
				BigDecimal distributorSettleAmount = distributorCommisionRateProductBean
						.getDistributorSettlementCurrencySettleAmount();

				subDistributorCommisionRateProductBean
						.setDistributorSettlementCurrencySettleAmount(distributorSettleAmount
								.multiply(subDistributorRate).setScale(2,
										BigDecimal.ROUND_DOWN));
				subDistributorCommisionRateProductBean
						.setDistributorProductBeanList(distributorProductBeanList);
				subDistributorCommisionRateProductBean
						.setRefundBillNoRemainsSettleAmountMap(distributorCommisionRateProductBean
								.getRefundBillNoRemainsSettleAmountMap());
			}

			chain.add(distributorCommisionRateProductBean);

			int delayDays = this.calDelayDays(distributionAgencyConfig);

			chain.stream().forEach(
					bean -> {
						bean.setSettlementDelayDays(delayDays);
						bean.setSettlementWay(distributionAgencyConfig
								.getSettlement_way());
					});
		} else {

			if (csId > 0) {
				Logger.comment("用户已经绑定了客服，分销员没有任何分销数据");
				return null;
			}

			Logger.comment("分销员分佣链条");

			distributorRelation = null;

			// 如果dsId > 0，则查询消费者是否为分销员
			if (Utils.zeroIfNull(dsId) > 0)
				distributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(sellerId,
								(long) dsId);

			// 如果消费者是分销员
			if (null != distributorRelation) {

				Long parentDistributorId = distributorRelation
						.getParent_distributor_id();

				// 如果允许自购
				if (distributionConfig.getSelf_purchase_commission()) {

					// 如果为最上级，则不参与分润
					while (parentDistributorId != 0) {

						// 如果消费者为分销员且允许自购，非清退状态
						if (distributorRelation.getDistributor_status() != 1)
							distributorRelationList.add(distributorRelation);

						distributorRelation = distributorRelationWrapper
								.selectByMerchantIdAndDistributorId(sellerId,
										parentDistributorId);

						parentDistributorId = distributorRelation
								.getParent_distributor_id();
					}
				} else {

					// 如果为最上级，则不参与分润
					while (parentDistributorId != 0) {

						distributorRelation = distributorRelationWrapper
								.selectByMerchantIdAndDistributorId(sellerId,
										parentDistributorId);

						// 如果消费者非清退状态
						if (distributorRelation.getDistributor_status() != 1
								&& distributorRelation
										.getParent_distributor_id() != 0)
							distributorRelationList.add(distributorRelation);

						parentDistributorId = distributorRelation
								.getParent_distributor_id();
					}
				}
			} else {

				if (distributorConsumerRelation == null) {

					Logger.comment("没有用户与分销员的关系，无法分佣");
					return null;
				}

				if (distributorConsumerRelation.getExpiration_date() != null
						&& YMTDateUtil.diffSecond(distributorConsumerRelation
								.getExpiration_date(), YMTDateUtil.getDate()) > 0) {
					Logger.comment("用户与分销员关系过期，无法分佣");
					return null;
				}

				// 如果消费者与分销员有绑定关系，则查询分销员
				distributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(sellerId,
								distributorConsumerRelation.getDistributor_id());

				Long parentDistributorId = distributorRelation
						.getParent_distributor_id();

				// 如果为最上级，则不参与分润
				while (parentDistributorId != 0) {

					// 如果非清退状态
					if (distributorRelation.getDistributor_status() != 1)
						distributorRelationList.add(distributorRelation);

					distributorRelation = distributorRelationWrapper
							.selectByMerchantIdAndDistributorId(sellerId,
									parentDistributorId);

					parentDistributorId = distributorRelation
							.getParent_distributor_id();
				}
			}

			if (distributorRelationList.size() != 0) {

				int layers = distributionConfig.getDistribution_layers();

				if (layers <= distributorRelationList.size()) {

					for (int i = 0; i < layers; i++) {

						DistributorRelation relation = distributorRelationList
								.get(i);

						if (i < layers - 1)
							relation.setParent_distributor_id(distributorRelationList
									.get(i + 1).getDistributor_id());

						if (i == layers - 1)
							relation.setParent_distributor_id(null);

						ArrayList<DistributorProductBean> distributorProductBeanList = new ArrayList<>();

						for (OrderDetailPo detailPo : orderDetailPoList) {

							DistributorProductBean distributorProductBean = new DistributorProductBean();
							distributorProductBean.setOrderDetailPo(detailPo);

							distributorProductBeanList
									.add(distributorProductBean);
						}

						chain.add(this
								.buildDistributorCommisionRateProductBean(
										relation, distributionConfig.getId(),
										i, distributorProductBeanList,
										paymentPo, orderSettlement, refundBillPoList));
					}

				} else {

					for (int i = 0; i < distributorRelationList.size(); i++) {

						DistributorRelation relation = distributorRelationList
								.get(i);

						if (i < distributorRelationList.size() - 1)
							relation.setParent_distributor_id(distributorRelationList
									.get(i + 1).getDistributor_id());

						if (i == distributorRelationList.size() - 1)
							relation.setParent_distributor_id(null);

						ArrayList<DistributorProductBean> distributorProductBeanList = new ArrayList<>();

						for (OrderDetailPo detailPo : orderDetailPoList) {

							DistributorProductBean distributorProductBean = new DistributorProductBean();
							distributorProductBean.setOrderDetailPo(detailPo);

							distributorProductBeanList
									.add(distributorProductBean);
						}

						chain.add(this
								.buildDistributorCommisionRateProductBean(
										relation, distributionConfig.getId(),
										i, distributorProductBeanList,
										paymentPo, orderSettlement, refundBillPoList));
					}
				}

				chain.stream().forEach(
						bean -> {
							bean.setSettlementWay(0);
							bean.setSettlementDelayDays(distributionConfig
									.getDistribution_settlement_delay_days());
						});

			} else
				return null;
		}

		return chain;
	}

	public DistributorCommisionRateProductBean buildPartnerCommisionRateProductBean(
			DistributorRelation relation, Long id, int index,
			ArrayList<DistributorProductBean> distributorProductBeanList,
			PaymentPo paymentPo, OrderSettlement orderSettlement, List<RefundBill> refundBillPoList) {

		DistributorCommisionRateProductBean bean = new DistributorCommisionRateProductBean();

		PartnerRewardRate partnerRewardRate = partnerRewardRateWrapper
				.selectByConfigId(id);

		bean.setDistributorRelation(relation);
		bean.setPartnerRewardRate(partnerRewardRate);

		if (index == 0)
			bean.setCurrencyRate(partnerRewardRate.getLayer1_reward_rate());
		else if (index == 1)
			bean.setCurrencyRate(partnerRewardRate.getLayer2_reward_rate());

		BigDecimal distributorBidCurrencySettleAmount = BigDecimal.ZERO;
		BigDecimal distributorSettlementCurrencySettleAmount = BigDecimal.ZERO;

		BigDecimal foreignVsBidExchangeRate = null;
		BigDecimal bidVsSettlemntExchangeRate = new BigDecimal(
				paymentPo.getExchangeRate() == null ? "1.00"
						: paymentPo.getExchangeRate());

		if (orderSettlement.getBid_currency().equalsIgnoreCase("CNY")
				|| !orderSettlement.getBid_currency().equalsIgnoreCase(
						orderSettlement.getCurrency())) {
			// 如果商品标价币种为人民币或货头的结算币种与标价币种不一致
			foreignVsBidExchangeRate = new BigDecimal(
					orderSettlement.getExchange_rate());
		} else {
			foreignVsBidExchangeRate = paymentPo.getShenqiPaymentRmbAmount()
					.multiply(
							BigDecimal.ONE.divide(
									paymentPo.getRealForeignAmount(), 7,
									BigDecimal.ROUND_DOWN));
		}

		if (null != distributorProductBeanList) {

			for (DistributorProductBean distributorProductBean : distributorProductBeanList) {

				OrderDetailPo orderDetailPo = distributorProductBean
						.getOrderDetailPo();
				BigDecimal currencyRate = BigDecimal.ZERO;

				// 如果存在指定商品分佣，则使用商品分佣，否则使用平台分佣
				ProductCommissionConfig productCommissionConfig = productCommissionConfigWrapper
						.selectByMerchantIdAndProductId(
								relation.getMerchant_id(),
								orderDetailPo.getProductId());

				String productId = orderDetailPo.getProductId();

				if (null == productCommissionConfig) {

					Logger.comment(String.format("指定商品%s，没有分佣，使用平台分佣",
							productId));

					currencyRate = bean.getCurrencyRate();
					distributorProductBean.setProductConfigId(null);

				} else {

					ProductCommissionRate productCommissionRate = productCommissionRateWrapper
							.selectByConfigIdAndTypeAndLevel(
									productCommissionConfig.getId(), 2, 0);

					if (null == productCommissionRate) {

						Logger.comment(String.format("指定商品%s，没有配置合伙人分佣，使用平台分佣",
								productId));

						currencyRate = bean.getCurrencyRate();
						distributorProductBean.setProductConfigId(null);
					} else {

						distributorProductBean
								.setProductCommissionRate(productCommissionRate);

						if (index == 0)
							distributorProductBean
									.setProductCurrencyRate(productCommissionRate
											.getLayer1_rate());
						else if (index == 1)
							distributorProductBean
									.setProductCurrencyRate(productCommissionRate
											.getLayer2_rate());
						else
							distributorProductBean
									.setProductCurrencyRate(productCommissionRate
											.getLayer3_rate());

						currencyRate = distributorProductBean
								.getProductCurrencyRate();
						distributorProductBean
								.setProductConfigId(productCommissionConfig
										.getId());
					}
				}

				distributorProductBean = buildDistributorProductBean(
						distributorProductBean, orderDetailPo, currencyRate,
						bidVsSettlemntExchangeRate);

				distributorBidCurrencySettleAmount = distributorBidCurrencySettleAmount
						.add(distributorProductBean
								.getBidCurrencySettleAmount());
				distributorSettlementCurrencySettleAmount = distributorSettlementCurrencySettleAmount
						.add(distributorProductBean
								.getSettlementCurrencySettleAmount());
			}
		}

		String foreignCurrency = orderSettlement.getCurrency();

		bean.setDistributorProductBeanList(distributorProductBeanList);
		bean.setDistributorBidCurrencySettleAmount(distributorBidCurrencySettleAmount);
		bean.setDistributorSettlementCurrencySettleAmount(distributorSettlementCurrencySettleAmount);
		bean.setBidVsSettlemntExchangeRate(bidVsSettlemntExchangeRate);
		bean.setForeignVsBidExchangeRate(foreignVsBidExchangeRate);
		bean.setPayChannelType(orderSettlement.getPay_channel_type());
		bean.setForeignCurrency(foreignCurrency);

		return bean;
	}

	/**
	 *
	 * @param merchantId
	 * @param distributorId
	 * @param orderDetailPoList
	 * @param paymentPo
	 * @param orderSettlement
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DistributorCommisionRateProductBean> buildPartnerChain(
			Long merchantId, Long distributorId,
			List<OrderDetailPo> orderDetailPoList, PaymentPo paymentPo,
			OrderSettlement orderSettlement, List<RefundBill> refundBillPoList) throws Exception {

		PartnerConfig partnerConfig = partnerConfigWrapper
				.selectByMerchantId(merchantId);

		if (null == partnerConfig) {

			Logger.comment("商户没有设置合伙人配置，没有合伙人奖励");
			return null;
		}

		ArrayList<DistributorCommisionRateProductBean> chain = new ArrayList<>();
		ArrayList<DistributorRelation> distributorRelationList = new ArrayList<>();

		// 合伙人最多有两个
		int maxPartnerNum = 2;

		// 如果消费者与分销员有绑定关系，则查询分销员
		DistributorRelation distributorRelation = distributorRelationWrapper
				.selectByMerchantIdAndDistributorId(merchantId, distributorId);

		Long parentDistributorId = distributorRelation
				.getParent_distributor_id();

		// 如果合伙人数量达到了2个
		while (distributorRelationList.size() < maxPartnerNum) {

			// 如果非清退状态
			if (distributorRelation.getDistributor_status() != 1
					&& distributorRelation.getPartner_type() == 1)
				distributorRelationList.add(distributorRelation);

			distributorRelation = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(merchantId,
							parentDistributorId);

			if (null == distributorRelation)
				break;

			parentDistributorId = distributorRelation
					.getParent_distributor_id();
		}

		for (int i = 0; i < distributorRelationList.size(); i++) {

			DistributorRelation relation = distributorRelationList.get(i);

			if (i < distributorRelationList.size() - 1)
				relation.setParent_distributor_id(distributorRelationList.get(
						i + 1).getDistributor_id());

			if (i == distributorRelationList.size() - 1)
				relation.setParent_distributor_id(null);

			ArrayList<DistributorProductBean> distributorProductBeanList = new ArrayList<>();

			for (OrderDetailPo detailPo : orderDetailPoList) {

				DistributorProductBean distributorProductBean = new DistributorProductBean();
				distributorProductBean.setOrderDetailPo(detailPo);

				distributorProductBeanList.add(distributorProductBean);
			}

			chain.add(this.buildPartnerCommisionRateProductBean(relation,
					partnerConfig.getId(), i, distributorProductBeanList,
					paymentPo, orderSettlement, refundBillPoList));
		}

		chain.stream().forEach(bean -> {
			bean.setSettlementWay(0);
		});

		return chain;
	}

	public DistributorProductBean buildDistributorProductBean(
			DistributorProductBean distributorProductBean,
			OrderDetailPo orderDetailPo, BigDecimal distributorRate,
			BigDecimal bidVsSettlemntExchangeRate) {

		BigDecimal productCount = new BigDecimal(
				orderDetailPo.getProductCount());

		BigDecimal productPayableAmount = TradingCallService
				.getOrderDetailPaidAmountExcludeFreight(orderDetailPo);

		BigDecimal perProductPayableAmount = productPayableAmount.divide(
				productCount, 7, BigDecimal.ROUND_DOWN);

		BigDecimal perProductFreightAmount = orderDetailPo
				.getFullReductionFreeFreight() ? BigDecimal.ZERO : Utils
				.zeroIfNull(orderDetailPo.getFreight()).divide(productCount, 2,
						BigDecimal.ROUND_DOWN);

		BigDecimal perProductTaxAmount = orderDetailPo.getTaxFee();

		BigDecimal bidCurrencySettleAmount = perProductPayableAmount
				.multiply(productCount).multiply(distributorRate)
				.setScale(2, BigDecimal.ROUND_DOWN);

		BigDecimal settlementCurrencySettleAmount = bidCurrencySettleAmount
				.multiply(bidVsSettlemntExchangeRate).setScale(2,
						BigDecimal.ROUND_DOWN);

		distributorProductBean
				.setPerProductFreightAmount(perProductFreightAmount);
		distributorProductBean
				.setPerProductPayableAmount(perProductPayableAmount.setScale(2,
						BigDecimal.ROUND_DOWN));
		distributorProductBean.setPerProductTaxAmount(perProductTaxAmount);
		distributorProductBean
				.setBidCurrencySettleAmount(bidCurrencySettleAmount.setScale(2,
						BigDecimal.ROUND_DOWN));
		distributorProductBean
				.setSettlementCurrencySettleAmount(settlementCurrencySettleAmount);
		distributorProductBean.setProductPayableAmount(perProductPayableAmount
				.multiply(productCount).setScale(2, BigDecimal.ROUND_DOWN));

		return distributorProductBean;
	}

	public void verify(long paymentId,
			HashMap<Long, BigDecimal> placeOrderProfitMap, boolean hpSettled,
			boolean settled) throws Exception {
		this.verify(paymentId, placeOrderProfitMap, hpSettled, settled, false);
	}

	public void verify(long paymentId,
			HashMap<Long, BigDecimal> placeOrderProfitMap, boolean hpSettled,
			boolean settled, Boolean isForeignExchangeBid) throws Exception {
		this.verify(paymentId, placeOrderProfitMap, hpSettled, settled,
				isForeignExchangeBid, 0);
	}

	public void verify(long paymentId,
			HashMap<Long, BigDecimal> placeOrderProfitMap, boolean hpSettled,
			boolean settled, Boolean isForeignExchangeBid, int refunded)
			throws Exception {

		List<PaymentDetailPo> paymentDetails = wapper
				.selectPaymentDetailByPaymentId(paymentId);

		if (paymentDetails.size() == 0)
			Logger.comment("没有找到支付信息，验证失败");

		verifyDb(paymentDetails, placeOrderProfitMap, hpSettled, settled,
				isForeignExchangeBid, refunded);
	}

	/**
	 *
	 * @param paymentDetails
	 * @param placeOrderProfitMap
	 * @param hpSettled
	 * @param settled
	 * @param isForeignExchangeBid
	 * @param refunded（0：不退款， 1：先退款后结算，2：先结算后退款）
	 * @throws Exception
	 */
	public void verifyDb(List<PaymentDetailPo> paymentDetails,
			HashMap<Long, BigDecimal> placeOrderProfitMap, Boolean hpSettled,
			Boolean settled, Boolean isForeignExchangeBid, int refunded)
			throws Exception {

		for (PaymentDetailPo paymentDetailPo : paymentDetails) {

			Long orderId = paymentDetailPo.getOrderId();
			Long paymentId = paymentDetailPo.getPaymentId();

			OrderPo orderPo = wapper.selectOrderbyOrderId(orderId);
			List<OrderDetailPo> orderDetailPoList = wapper
					.selectOrderDetailbyOrderId(orderId);
			PaymentPo paymentPo = wapper.selectPaymentById(paymentId);

			Long userId = orderPo.getUserId();
			Long sellerId = orderPo.getSellerId();
			Long goodsOwnerId = orderPo.getGoodsOwnerId();

            // 盛付通分账并结算
            if (settled && orderPo.getPayChannelType() == 1) {

                ShenqiSettlementService.executeShengPayMerchantSharingConvergeTask();
                orderSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(orderId, 1, 0);
                ShenqiSettlementService.doSettlement();
            }

			OrderSettlement orderSettlement = orderSettlementWrapper
					.selectByPaymentIdAndOrderId(paymentId, orderId);

			// 平台供货商订单
			boolean isPlatformSupplierOrder = (Utils.zeroIfNull(orderPo
					.getOrderFlags()) & 1 << 2) > 0;

			List<RefundBill> refundBillPoList = null;

			if (refunded > 0) {

				refundBillPoList = refundBillWrapper.selectByOrderId(orderId);

				refundBillPoList = refundBillPoList.stream()
						.sorted(Comparator.comparing(RefundBill::getAdd_time))
						.collect(Collectors.toList());
			}

			ArrayList<DistributorCommisionRateProductBean> distributorCommisionRateProductBeanList = this
					.buildDistributorChain(sellerId, userId, orderDetailPoList,
							paymentPo, orderSettlement,
							Utils.zeroIfNull(orderPo.getSalesman()), refundBillPoList);
			ArrayList<DistributorCommisionRateProductBean> partnerCommisionRateProductBeanList = null;

			// 直属上级
			DistributorRelation directDistributor = distributorCommisionRateProductBeanList
					.get(0).getDistributorRelation();

			Boolean isCanceled = TradingCallService.isCanceledOrder(orderPo);

			int delayDays = distributorCommisionRateProductBeanList.get(0)
					.getSettlementDelayDays();
			int settlementWay = distributorCommisionRateProductBeanList.get(0)
					.getSettlementWay();

			// 去除子分销员的金额
			BigDecimal commissionAmount = distributorCommisionRateProductBeanList
					.stream()
					.filter(distributorCommisionRateProductBean -> !this
							.isSubDistributor(distributorCommisionRateProductBean
									.getDistributorRelation()
									.getDistributor_type()))
					.map(distributorCommisionRateProductBean -> distributorCommisionRateProductBean
							.getDistributorSettlementCurrencySettleAmount())
					.reduce(BigDecimal.ZERO, BigDecimal::add)
					.setScale(2, BigDecimal.ROUND_DOWN);
			BigDecimal bidCommissionAmount = distributorCommisionRateProductBeanList
					.stream()
					.filter(distributorCommisionRateProductBean -> !this
							.isSubDistributor(distributorCommisionRateProductBean
									.getDistributorRelation()
									.getDistributor_type()))
					.map(distributorCommisionRateProductBean -> distributorCommisionRateProductBean
							.getDistributorBidCurrencySettleAmount())
					.reduce(BigDecimal.ZERO, BigDecimal::add)
					.setScale(2, BigDecimal.ROUND_DOWN);

			// 合伙人的奖金
			BigDecimal bonusAmount = null;
			BigDecimal bidBonusAmount = null;

			// 只有第一个人是普通分销员，才需要获取合伙人
			if (directDistributor.getDistributor_type() == 0) {

				partnerCommisionRateProductBeanList = this.buildPartnerChain(
						sellerId, directDistributor.getDistributor_id(),
						orderDetailPoList, paymentPo, orderSettlement,
						refundBillPoList);

				if (null == partnerCommisionRateProductBeanList) {

					Logger.comment("没有合伙人奖金");
				} else {

					bonusAmount = partnerCommisionRateProductBeanList
							.stream()
							.map(partnerCommisionRateProductBean -> partnerCommisionRateProductBean
									.getDistributorSettlementCurrencySettleAmount())
							.reduce(BigDecimal.ZERO, BigDecimal::add)
							.setScale(2, BigDecimal.ROUND_DOWN);
					bidBonusAmount = partnerCommisionRateProductBeanList
							.stream()
							.map(partnerCommisionRateProductBean -> partnerCommisionRateProductBean
									.getDistributorBidCurrencySettleAmount())
							.reduce(BigDecimal.ZERO, BigDecimal::add)
							.setScale(2, BigDecimal.ROUND_DOWN);
				}
			}

//			if (orderSettlement.getSettlement_status() == 0)
//				if (!settled && isCanceled)
//					settlementStatus = 4;
//				else
//					settlementStatus = 0;

            int settlementStatus = this.getSettlementStatus(isCanceled,
                    settled, delayDays, refundBillPoList, orderId, orderSettlement, commissionAmount, bonusAmount);

			if (settlementStatus == 1 && orderPo.getPayChannelType() == 1) {

				ShenqiSettlementService.executeShengPayMerchantSharingConvergeTask();

				orderSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(
						orderId, 1, 2);
				distributorSettlementWrapper.updateShengpaySharingExecuteStatusByOrderId(
						orderId, 1);
				// 分销员结算
				SqDistributorService.doDistributorSettlement();
			}

			DistributorSettlement expectDistributorSettlement = new DistributorSettlement();
			DistributorSettlement actualDistributorSettlement = distributorSettlementWrapper
					.selectByPaymentIdAndOrderId(paymentId, orderId);

			expectDistributorSettlement.setOrder_id(orderId);
			expectDistributorSettlement.setPayment_id(paymentId);
			expectDistributorSettlement.setUser_id(userId);
			expectDistributorSettlement.setSeller_id(sellerId);
			expectDistributorSettlement.setPayable_amount(orderSettlement
					.getTotal_amount());
			expectDistributorSettlement.setBid_currency(orderSettlement
					.getBid_currency());
			expectDistributorSettlement.setCommission_amount(commissionAmount);
			expectDistributorSettlement
					.setBid_commission_amount(bidCommissionAmount);
			expectDistributorSettlement
					.setDirect_distributor_id(directDistributor
							.getDistributor_id());
			expectDistributorSettlement
					.setDirect_distributor_name(directDistributor
							.getDistributor_name());
			expectDistributorSettlement.setSettlement_status(settlementStatus);
			expectDistributorSettlement.setHp_settled(hpSettled ? true : false);
			expectDistributorSettlement
					.setCustomer_payment_order_no(orderSettlement
							.getCustomer_payment_order_no());
			expectDistributorSettlement
					.setCustomer_payment_order_amount(orderSettlement
							.getCustomer_payment_order_amount());

			expectDistributorSettlement.setPay_channel_type(orderSettlement
					.getPay_channel_type());
			expectDistributorSettlement.setDistributor_type(directDistributor
					.getDistributor_type());

			expectDistributorSettlement.setSettlement_way(settlementWay);

			expectDistributorSettlement.setBonus_amount(bonusAmount);
			expectDistributorSettlement.setBid_bonus_amount(bidBonusAmount);

			if (expectDistributorSettlement.getSettlement_status() == 1) {

				if (settlementWay == 0)
					expectDistributorSettlement.setSettlement_result("0");
				else
					expectDistributorSettlement.setSettlement_result("");

			} else if (expectDistributorSettlement.getSettlement_status() == 2
					&& commissionAmount.compareTo(BigDecimal.ZERO) == 0
					&& Utils.zeroIfNull(bonusAmount).compareTo(BigDecimal.ZERO) == 0)
				expectDistributorSettlement
						.setSettlement_result("订单分销总佣金为零并且合伙人奖金为零，无需结算");
			else
				expectDistributorSettlement.setSettlement_result(null);

			// 验证order_settlement
			DataUtil.verifyDecimalEquals(commissionAmount,
					orderSettlement.getTotal_distributor_commission_amount(),
					"验证order_settlement的Total_distributor_commission_amount");

			// 获取货头是资金归集模式
			MerchantFundCollectConfigResp merchantFundCollectConfigResp = GodmchManageCallService
					.getMerchantFundCollectConfig(goodsOwnerId);

			Boolean fundNotCollect = merchantFundCollectConfigResp
					.getFundNotCollect();
			Long fundNotCollectMerchantId = null;

			if (Utils.falseIfNull(fundNotCollect))
				fundNotCollectMerchantId = merchantFundCollectConfigResp
						.getNotCollectRootMerchantId();

			this.buildDistributorSettlementDetailForVerify(
					expectDistributorSettlement, delayDays,
					distributorCommisionRateProductBeanList, refundBillPoList,
					fundNotCollectMerchantId, refunded, isPlatformSupplierOrder);

			// 验证order_settlement_detail
			HashMap<String, BigDecimal> refundedMap = this
					.verifyOrderSettlementDetailForDistributor(
							expectDistributorSettlement, refundBillPoList);

			/**
			 * 如果发生了退货退款，会修改distributor_settlement表中的标价金额和结算金额，
			 * 所以先使用退货退款前的标价金额和结算金额验证其他表的数据，然后减去退货退款的标价金额和结算金额
			 */
			if (null != refundBillPoList
					&& expectDistributorSettlement.getCommission_amount()
							.compareTo(BigDecimal.ZERO) == 1) {

				BigDecimal totalRefundedCommissionAmount = refundedMap.get(
						"totalRefundedCommissionAmount").negate();
				BigDecimal totalRefundedBidCommission = refundedMap
						.get("totalRefundedBidCommission");

				expectDistributorSettlement
						.setBid_commission_amount(expectDistributorSettlement
								.getBid_commission_amount().subtract(
										totalRefundedBidCommission));
				expectDistributorSettlement
						.setCommission_amount(expectDistributorSettlement
								.getCommission_amount().subtract(
										totalRefundedCommissionAmount));
			}

			if (null != partnerCommisionRateProductBeanList
					&& bonusAmount.compareTo(BigDecimal.ZERO) == 1) {

				this.buildPartnerDistributorSettlementDetailForVerify(
						expectDistributorSettlement, delayDays,
						partnerCommisionRateProductBeanList, refundBillPoList,
						fundNotCollectMerchantId, isPlatformSupplierOrder);

				// 验证order_settlement_detail
				HashMap<String, BigDecimal> partnerRefundedMap = this
						.verifyOrderSettlementDetailForPartner(
								expectDistributorSettlement, refundBillPoList);

				/**
				 * 如果发生了退货退款，会修改distributor_settlement表中的标价金额和结算金额，
				 * 所以先使用退货退款前的标价金额和结算金额验证其他表的数据，然后减去退货退款的标价金额和结算金额
				 */
				if (null != refundBillPoList
						&& expectDistributorSettlement.getBonus_amount()
								.compareTo(BigDecimal.ZERO) == 1) {

					BigDecimal totalRefundedCommissionAmount = partnerRefundedMap
							.get("totalRefundedCommissionAmount").negate();
					BigDecimal totalRefundedBidCommission = partnerRefundedMap
							.get("totalRefundedBidCommission");

					expectDistributorSettlement
							.setBid_bonus_amount(expectDistributorSettlement
									.getBid_bonus_amount().subtract(
											totalRefundedBidCommission));
					expectDistributorSettlement
							.setBonus_amount(expectDistributorSettlement
									.getBonus_amount().subtract(
											totalRefundedCommissionAmount));
				}
			}

			// 如果是子分销员，传入分销机构/CPS的信息
			if (this.isSubDistributor(expectDistributorSettlement
					.getDistributor_type())) {

				DistributorRelation parentDistributor = distributorCommisionRateProductBeanList
						.get(1).getDistributorRelation();

				expectDistributorSettlement
						.setDirect_distributor_id(parentDistributor
								.getDistributor_id());
				expectDistributorSettlement
						.setDirect_distributor_name(parentDistributor
								.getDistributor_name());
				expectDistributorSettlement
						.setDistributor_type(parentDistributor
								.getDistributor_type());
			}

			this.verifyDistributorSettlementTable(expectDistributorSettlement,
					actualDistributorSettlement, delayDays, settled);
		}
	}

	/**
	 * 分销员OrderSettlementDetail
	 *
	 * @param distributorSettlement
	 * @param refundBillPoList
	 * @return
	 */
	public HashMap<String, BigDecimal> verifyOrderSettlementDetailForDistributor(
			DistributorSettlement distributorSettlement,
			List<RefundBill> refundBillPoList) {

		long orderId = distributorSettlement.getOrder_id();
		long sellerId = distributorSettlement.getSeller_id();

		// 店主分销扣款明细
		OrderSettlementDetail actualOrderSettlementDetail = orderSettlementDetailWrapper
				.selectDistributorByOrderIdAndUserId(orderId, sellerId);

		if (distributorSettlement.getBid_commission_amount().compareTo(
				BigDecimal.ZERO) == 0) {

			Logger.verifyEquals(null, actualOrderSettlementDetail,
					"分销金额为0，order_settlement_detail没有数据");
			return null;
		} else {
			Logger.comment(String
					.format("验证order_settlement_detail分销扣款单，orderId：%s，userId：%s，bizId：%s",
							orderId, sellerId, orderId));

			OrderSettlementDetail settlemntDetail = orderSettlementDetailWrapper
					.selectByOrderIdAndUserId(orderId, sellerId);

			OrderSettlementDetail expectOrderSettlementDetail = new OrderSettlementDetail();

			BigDecimal bidCommissionAmount = distributorSettlement
					.getBid_commission_amount();

			BigDecimal settlementAmount = bidCommissionAmount.multiply(
					settlemntDetail.getBid_vs_settlement_exchange_rate())
					.negate();

			expectOrderSettlementDetail.setTotal_rmb_amount(bidCommissionAmount
					.negate());
			expectOrderSettlementDetail
					.setTotal_foreign_amount(BigDecimal.ZERO);
			expectOrderSettlementDetail.setSettlement_amount(settlementAmount);
			expectOrderSettlementDetail.setBiz_id(String.valueOf(orderId));
			expectOrderSettlementDetail.setSettlement_type(2);
			expectOrderSettlementDetail.setSettlement_way(distributorSettlement
					.getSettlement_way());

			// 分销订单结算状态，如果整单取消，status为4，否则OrderSettlementDetail跟着distributorSettlement
			if (distributorSettlement.getSettlement_status() == 4)
				expectOrderSettlementDetail.setSettlement_status(2);
			else
				expectOrderSettlementDetail
						.setSettlement_status(distributorSettlement
								.getSettlement_status());

			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getTotal_rmb_amount(),
					actualOrderSettlementDetail.getTotal_rmb_amount(),
					"验证Total_rmb_amount");
			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getTotal_foreign_amount(),
					actualOrderSettlementDetail.getTotal_foreign_amount(),
					"验证Total_foreign_amount");
			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getSettlement_amount(),
					actualOrderSettlementDetail.getSettlement_amount(),
					"验证Settlement_amount");
			Logger.verifyEquals(expectOrderSettlementDetail.getBiz_id(),
					actualOrderSettlementDetail.getBiz_id(), "验证Biz_id");
			Logger.verifyEquals(2,
					actualOrderSettlementDetail.getSettlement_type(),
					"验证Settlement_type");
			Logger.verifyEquals(
					expectOrderSettlementDetail.getSettlement_status(),
					actualOrderSettlementDetail.getSettlement_status(),
					"验证Settlement_status");

			int settlementWay = Utils.zeroIfNull(distributorSettlement
					.getSettlement_way());

			// 校验资金流水
			if (actualOrderSettlementDetail.getSettlement_status() == 1
					&& settlementWay == 0 && actualOrderSettlementDetail.getPay_channel_type() != 5
					&& actualOrderSettlementDetail.getPay_channel_type() != 61) {

				int accountType = ShenqiSettlementBuildVerifyService
						.getAccountType(
								actualOrderSettlementDetail
										.getPay_channel_type(),
								actualOrderSettlementDetail
										.getSettlement_currency(),
								Utils.zeroIfNull(actualOrderSettlementDetail
										.getFund_not_collect_merchant_id()) > 0);

				Logger.comment("校验资金账户信息");
				AccountWapper accountWapper = new AccountWapper();
				HashMap<String, Object> accountEntryMap = new HashMap<>();

				accountEntryMap.put("userId", sellerId);
				accountEntryMap.put("bizcode", "360036");
				accountEntryMap.put("accountType", accountType);

//				if (actualOrderSettlementDetail.getSettlement_currency()
//						.equalsIgnoreCase("CNY"))
//					accountEntryMap
//							.put("accountType", actualOrderSettlementDetail
//									.getPay_channel_type() == 0 ? 61 : 89);
//				else if (actualOrderSettlementDetail.getSettlement_currency()
//						.equalsIgnoreCase("USD"))
//					accountEntryMap
//							.put("accountType", actualOrderSettlementDetail
//									.getPay_channel_type() == 0 ? 69 : 89);
//				else if (actualOrderSettlementDetail.getSettlement_currency()
//						.equalsIgnoreCase("JPY"))
//					accountEntryMap.put("accountType", 70);
//				else
//					accountEntryMap.put("accountType", 63);

				accountEntryMap.put("originalNo", orderId);
				accountEntryMap.put("bizno", orderId);

				DataUtil.verifyDecimalEquals(settlementAmount, accountWapper
						.selectAccountEntryByMap(accountEntryMap).get(0)
						.getAmount(), "验证accountEntry日志记录");
			}

			BigDecimal totalRefundedCommissionAmount = BigDecimal.ZERO;
			BigDecimal totalRefundedBidCommission = BigDecimal.ZERO;

			HashMap<String, BigDecimal> refundedMap = new HashMap<>();

			if (refundBillPoList != null) {

				for (RefundBill refundBillPo : refundBillPoList) {

					OrderSettlementDetail actualRefundedOrderSettlementDetail = orderSettlementDetailWrapper
							.selectRefundedDistributorByOrderIdAndUserId(
									orderId, sellerId,
									refundBillPo.getRefund_no());

					if (settlemntDetail.getSettlement_status() == 2) {

						Logger.verifyEquals(null,
								actualRefundedOrderSettlementDetail,
								"订单取消或整单退款后，order_settlement_detail没有数据");
					} else {

						String bizId = refundBillPo.getRefund_no();

						List<DistributorSettlementDetail> distributorSettlementDetailList = distributorSettlementDetailWrapper
								.selectRefundedByPaymentIdAndOrderId(
										distributorSettlement.getPayment_id(),
										orderId, bizId);

						Logger.comment(String
								.format("验证order_settlement_detail分销返款单， orderId：%s；userId：%s；bizId：%s",
										orderId, sellerId, bizId));

						BigDecimal totalRefundedSettlementAmount = distributorSettlementDetailList
								.stream()
								.filter(distributorSettlementDetail -> !this
										.isSubDistributor(distributorSettlementDetail
												.getDistributor_type()))
								.map(distributorSettlementDetail -> distributorSettlementDetail
										.getSettlement_amount())
								.reduce(BigDecimal.ZERO, BigDecimal::add);
						bidCommissionAmount = totalRefundedSettlementAmount
								.multiply(
										distributorSettlementDetailList
												.get(0)
												.getBid_vs_settlement_exchange_rate())
								.negate();

						settlementAmount = bidCommissionAmount
								.multiply(settlemntDetail
										.getBid_vs_settlement_exchange_rate());

						DataUtil.verifyDecimalEquals(
								actualRefundedOrderSettlementDetail
										.getTotal_rmb_amount(),
								bidCommissionAmount, "验证Total_rmb_amount");
						DataUtil.verifyDecimalEquals(
								actualRefundedOrderSettlementDetail
										.getSettlement_amount(),
								settlementAmount, "验证Settlement_amount");
						Logger.verifyEquals(
								actualRefundedOrderSettlementDetail.getBiz_id(),
								refundBillPo.getRefund_no(), "验证biz_id");
						Logger.verifyEquals(actualRefundedOrderSettlementDetail
								.getSettlement_type(), 3, "验证Settlement_type");

						// 校验资金流水
						if (actualRefundedOrderSettlementDetail
								.getSettlement_status() == 1
								&& settlementWay == 0 && actualRefundedOrderSettlementDetail.getPay_channel_type() != 5) {

							int accountType = ShenqiSettlementBuildVerifyService
									.getAccountType(
											actualOrderSettlementDetail
													.getPay_channel_type(),
											actualOrderSettlementDetail
													.getSettlement_currency(),
											Utils.zeroIfNull(actualOrderSettlementDetail
													.getFund_not_collect_merchant_id()) > 0);

							Logger.comment("校验退款时的资金账户信息");
							AccountWapper accountWapper = new AccountWapper();
							HashMap<String, Object> accountEntryMap = new HashMap<>();

							accountEntryMap.put("userId", sellerId);
							accountEntryMap.put("bizcode", "360036");
							accountEntryMap.put("accountType", accountType);
							accountEntryMap.put("bizno", bizId);

							DataUtil.verifyDecimalEquals(
									settlementAmount,
									accountWapper
											.selectAccountEntryByMap(
													accountEntryMap).get(0)
											.getAmount(), "验证accountEntry日志记录");
						}

						totalRefundedCommissionAmount = totalRefundedCommissionAmount
								.add(totalRefundedSettlementAmount);
						totalRefundedBidCommission = totalRefundedBidCommission
								.add(bidCommissionAmount);
					}
				}
			}

			refundedMap.put("totalRefundedCommissionAmount",
					totalRefundedCommissionAmount);
			refundedMap.put("totalRefundedBidCommission",
					totalRefundedBidCommission);

			return refundedMap;
		}
	}

	/**
	 * 合伙人OrderSettlementDetail
	 *
	 * @param distributorSettlement
	 * @param refundBillPoList
	 * @return
	 */
	public HashMap<String, BigDecimal> verifyOrderSettlementDetailForPartner(
			DistributorSettlement distributorSettlement,
			List<RefundBill> refundBillPoList) {

		long orderId = distributorSettlement.getOrder_id();
		long sellerId = distributorSettlement.getSeller_id();

		// 店主合伙人扣款明细
		OrderSettlementDetail actualOrderSettlementDetail = orderSettlementDetailWrapper
				.selectPartnerDistributorByOrderIdAndUserId(orderId, sellerId);

		if (distributorSettlement.getBid_bonus_amount().compareTo(
				BigDecimal.ZERO) == 0) {

			Logger.verifyEquals(null, actualOrderSettlementDetail,
					"合伙人奖励金额为0，order_settlement_detail没有数据");
			return null;
		} else {
			Logger.comment(String
					.format("验证order_settlement_detail合伙人扣款单，orderId：%s，userId：%s，bizId：%s",
							orderId, sellerId, orderId));

			OrderSettlementDetail settlemntDetail = orderSettlementDetailWrapper
					.selectByOrderIdAndUserId(orderId, sellerId);

			OrderSettlementDetail expectOrderSettlementDetail = new OrderSettlementDetail();

			BigDecimal bidBonusAmount = distributorSettlement
					.getBid_bonus_amount();

			BigDecimal settlementAmount = bidBonusAmount.multiply(
					settlemntDetail.getBid_vs_settlement_exchange_rate())
					.negate();

			int settlementWay = Utils.zeroIfNull(distributorSettlement
					.getSettlement_way());

			expectOrderSettlementDetail.setTotal_rmb_amount(bidBonusAmount
					.negate());
			expectOrderSettlementDetail
					.setTotal_foreign_amount(BigDecimal.ZERO);
			expectOrderSettlementDetail.setSettlement_amount(settlementAmount);
			expectOrderSettlementDetail.setBiz_id(String.valueOf(orderId));
			expectOrderSettlementDetail.setSettlement_type(7);
			expectOrderSettlementDetail.setSettlement_way(settlementWay);

			// 合伙人订单结算状态，如果整单取消，status为4，否则OrderSettlementDetail跟着distributorSettlement
			if (distributorSettlement.getSettlement_status() == 4)
				expectOrderSettlementDetail.setSettlement_status(2);
			else
				expectOrderSettlementDetail
						.setSettlement_status(distributorSettlement
								.getSettlement_status());

			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getTotal_rmb_amount(),
					actualOrderSettlementDetail.getTotal_rmb_amount(),
					"验证Total_rmb_amount");
			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getTotal_foreign_amount(),
					actualOrderSettlementDetail.getTotal_foreign_amount(),
					"验证Total_foreign_amount");
			DataUtil.verifyDecimalEquals(
					expectOrderSettlementDetail.getSettlement_amount(),
					actualOrderSettlementDetail.getSettlement_amount(),
					"验证Settlement_amount");
			Logger.verifyEquals(expectOrderSettlementDetail.getBiz_id(),
					actualOrderSettlementDetail.getBiz_id(), "验证Biz_id");
			Logger.verifyEquals(
					expectOrderSettlementDetail.getSettlement_type(),
					actualOrderSettlementDetail.getSettlement_type(),
					"验证Settlement_type");
			Logger.verifyEquals(
					expectOrderSettlementDetail.getSettlement_status(),
					actualOrderSettlementDetail.getSettlement_status(),
					"验证Settlement_status");

			// 校验资金流水
			if (actualOrderSettlementDetail.getSettlement_status() == 1
					&& settlementWay == 0) {

				int accountType = ShenqiSettlementBuildVerifyService
						.getAccountType(
								actualOrderSettlementDetail
										.getPay_channel_type(),
								actualOrderSettlementDetail
										.getSettlement_currency(),
								Utils.zeroIfNull(actualOrderSettlementDetail
										.getFund_not_collect_merchant_id()) > 0);

				Logger.comment("校验资金账户信息");
				AccountWapper accountWapper = new AccountWapper();
				HashMap<String, Object> accountEntryMap = new HashMap<>();

				accountEntryMap.put("userId", sellerId);
				accountEntryMap.put("bizcode", "370038");
				accountEntryMap.put("accountType", accountType);

				accountEntryMap.put("originalNo", orderId);
				accountEntryMap.put("bizno", orderId);

				DataUtil.verifyDecimalEquals(settlementAmount, accountWapper
						.selectAccountEntryByMap(accountEntryMap).get(0)
						.getAmount(), "验证accountEntry日志记录");
			}

			BigDecimal totalRefundedBonusAmount = BigDecimal.ZERO;
			BigDecimal totalRefundedBidBonusCommission = BigDecimal.ZERO;

			HashMap<String, BigDecimal> refundedMap = new HashMap<>();

			if (refundBillPoList != null) {

				for (RefundBill refundBillPo : refundBillPoList) {

					OrderSettlementDetail actualRefundedOrderSettlementDetail = orderSettlementDetailWrapper
							.selectRefundedPartnerDistributorByOrderIdAndUserId(
									orderId, sellerId,
									refundBillPo.getRefund_no());

					if (settlemntDetail.getSettlement_status() == 2) {

						Logger.verifyEquals(null,
								actualRefundedOrderSettlementDetail,
								"订单取消或整单退款后，order_settlement_detail没有数据");
					} else {

						String bizId = refundBillPo.getRefund_no();

						List<DistributorSettlementDetail> distributorSettlementDetailList = distributorSettlementDetailWrapper
								.selectPartnerRefundedByPaymentIdAndOrderId(
										distributorSettlement.getPayment_id(),
										orderId, bizId);

						Logger.comment(String
								.format("验证order_settlement_detail合伙人返款单， orderId：%s；userId：%s；bizId：%s",
										orderId, sellerId, bizId));

						BigDecimal totalRefundedSettlementAmount = distributorSettlementDetailList
								.stream()
								.filter(distributorSettlementDetail -> !this
										.isSubDistributor(distributorSettlementDetail
												.getDistributor_type()))
								.map(distributorSettlementDetail -> distributorSettlementDetail
										.getSettlement_amount())
								.reduce(BigDecimal.ZERO, BigDecimal::add);
						bidBonusAmount = totalRefundedSettlementAmount
								.multiply(
										distributorSettlementDetailList
												.get(0)
												.getBid_vs_settlement_exchange_rate())
								.negate();

						settlementAmount = bidBonusAmount
								.multiply(settlemntDetail
										.getBid_vs_settlement_exchange_rate());

						DataUtil.verifyDecimalEquals(
								actualRefundedOrderSettlementDetail
										.getTotal_rmb_amount(), bidBonusAmount,
								"验证Total_rmb_amount");
						DataUtil.verifyDecimalEquals(
								actualRefundedOrderSettlementDetail
										.getSettlement_amount(),
								settlementAmount, "验证Settlement_amount");
						Logger.verifyEquals(
								actualRefundedOrderSettlementDetail.getBiz_id(),
								refundBillPo.getRefund_no(), "验证biz_id");
						Logger.verifyEquals(actualRefundedOrderSettlementDetail
								.getSettlement_type(), 8, "验证Settlement_type");

						// 校验资金流水
						if (actualRefundedOrderSettlementDetail
								.getSettlement_status() == 1
								&& settlementWay == 0) {

							int accountType = ShenqiSettlementBuildVerifyService
									.getAccountType(
											actualOrderSettlementDetail
													.getPay_channel_type(),
											actualOrderSettlementDetail
													.getSettlement_currency(),
											Utils.zeroIfNull(actualOrderSettlementDetail
													.getFund_not_collect_merchant_id()) > 0);

							Logger.comment("校验退款时的资金账户信息");
							AccountWapper accountWapper = new AccountWapper();
							HashMap<String, Object> accountEntryMap = new HashMap<>();

							accountEntryMap.put("userId", sellerId);
							accountEntryMap.put("bizcode", "370038");

							accountEntryMap.put("accountType", accountType);

							accountEntryMap.put("bizno", bizId);

							DataUtil.verifyDecimalEquals(
									settlementAmount,
									accountWapper
											.selectAccountEntryByMap(
													accountEntryMap).get(0)
											.getAmount(), "验证accountEntry日志记录");
						}

						totalRefundedBonusAmount = totalRefundedBonusAmount
								.add(totalRefundedSettlementAmount);
						totalRefundedBidBonusCommission = totalRefundedBidBonusCommission
								.add(bidBonusAmount);
					}
				}
			}

			refundedMap.put("totalRefundedCommissionAmount",
					totalRefundedBonusAmount);
			refundedMap.put("totalRefundedBidCommission",
					totalRefundedBidBonusCommission);

			return refundedMap;
		}
	}

	/**
	 * 分销员
	 *
	 * @param distributorSettlement
	 * @param delayDays
	 * @param distributorCommisionRateProductBeanList
	 * @param refundBillPoList
	 * @throws CloneNotSupportedException
	 */
	public void buildDistributorSettlementDetailForVerify(
			DistributorSettlement distributorSettlement,
			int delayDays,
			ArrayList<DistributorCommisionRateProductBean> distributorCommisionRateProductBeanList,
			List<RefundBill> refundBillPoList, Long fundNotCollectMerchantId, int refunded, Boolean isPlatformSupplierOrder)
			throws CloneNotSupportedException {

		DistributorSettlementDetail subDistributorSettlementDetail = null;

		for (DistributorCommisionRateProductBean distributorCommisionRateProductBean : distributorCommisionRateProductBeanList) {

			DistributorRelation distributorRelation = distributorCommisionRateProductBean
					.getDistributorRelation();

			CommisionRate commisionRate = null;
			DistributionAgencyCommisionRate distributionAgencyCommisionRate = null;
			DistributionAgencySubDistributorCommisionRate distributionAgencySubDistributorCommisionRate = null;

			Integer distributionConfigId = null;
			String distributorLevelLabel = null;

			Long parentDistributorId = null;
			Long originDistributorId = distributorCommisionRateProductBean
					.getOriginDistributorId();
			String originDistributorName = distributorCommisionRateProductBean
					.getOriginDistributorName();

			if (Utils.zeroIfNull(distributorRelation.getDistributor_type()) == 0) {

				// 普通分销员
				commisionRate = distributorCommisionRateProductBean
						.getCommisionRate();
				distributionConfigId = commisionRate
						.getDistribution_config_id().intValue();
				distributorLevelLabel = commisionRate
						.getDistributor_level_label();
				parentDistributorId = distributorRelation
						.getParent_distributor_id();
			} else if (Utils.zeroIfNull(distributorRelation
					.getDistributor_type()) == 1) {

				// 分销机构
				distributionAgencyCommisionRate = distributorCommisionRateProductBean
						.getDistributionAgencyCommisionRate();
				distributionConfigId = distributionAgencyCommisionRate
						.getDistribution_config_id().intValue();
				distributorLevelLabel = distributionAgencyCommisionRate
						.getDistributor_level_label();
			} else if (Utils.zeroIfNull(distributorRelation
					.getDistributor_type()) == 2) {

				// CPS
				distributionAgencyCommisionRate = distributorCommisionRateProductBean
						.getDistributionAgencyCommisionRate();
				distributionConfigId = distributionAgencyCommisionRate
						.getDistribution_config_id().intValue();
				distributorLevelLabel = distributionAgencyCommisionRate
						.getDistributor_level_label();
			} else if (this.isSubDistributor(distributorRelation
					.getDistributor_type())) {

				// 分销机构/CPS子分销员
				distributionAgencySubDistributorCommisionRate = distributorCommisionRateProductBean
						.getDistributionAgencySubDistributorCommisionRate();
				distributionConfigId = distributionAgencySubDistributorCommisionRate
						.getDistribution_agency_sub_distributor_config_id()
						.intValue();
				distributorLevelLabel = distributionAgencySubDistributorCommisionRate
						.getDistributor_level_label();
				parentDistributorId = distributorRelation
						.getParent_distributor_id();
			}

			int settlementStatus = 0;

			if (distributorSettlement.getSettlement_status() == 4)
				settlementStatus = 4;
			else if (distributorSettlement.getSettlement_status() == 1)
				settlementStatus = 1;
			else
				if (distributorSettlement.getCommission_amount().compareTo(BigDecimal.ZERO) == 0
						&& Utils.zeroIfNull(distributorSettlement.getBonus_amount()).compareTo(BigDecimal.ZERO) == 0)
					settlementStatus = 2;

			Long orderId = distributorSettlement.getOrder_id();
			Long paymentId = distributorSettlement.getPayment_id();

			int payChannelType = distributorCommisionRateProductBean
					.getPayChannelType();
			Long distributorId = distributorRelation.getDistributor_id();
			Integer distributorType = distributorRelation.getDistributor_type();

			BigDecimal settleAmount = distributorCommisionRateProductBean
					.getDistributorSettlementCurrencySettleAmount();
			String foreignCurrency = distributorCommisionRateProductBean
					.getForeignCurrency();
			Integer settlementWay = Utils.zeroIfNull(distributorSettlement
					.getSettlement_way());

			BigDecimal foreignAmount = BigDecimal.ZERO;

			DistributorSettlementDetail expectDistributorSettlementDetail = new DistributorSettlementDetail();

			expectDistributorSettlementDetail.setOrder_id(orderId);
			expectDistributorSettlementDetail.setPayment_id(paymentId);
			expectDistributorSettlementDetail.setUser_id(distributorSettlement
					.getUser_id());
			expectDistributorSettlementDetail
					.setSeller_id(distributorSettlement.getSeller_id());
			expectDistributorSettlementDetail.setDistributor_id(distributorId);
			expectDistributorSettlementDetail
					.setDistribution_config_id(distributionConfigId);
			expectDistributorSettlementDetail
					.setDistributor_name(distributorRelation
							.getDistributor_name());
			expectDistributorSettlementDetail
					.setDistributor_level(distributorRelation
							.getDistributor_level_index());
			expectDistributorSettlementDetail
					.setDistributor_level_label(distributorLevelLabel);
			expectDistributorSettlementDetail
					.setDistributor_phone(distributorRelation
							.getDistributor_phone());
			expectDistributorSettlementDetail
					.setDistributor_rate(distributorCommisionRateProductBean
							.getCurrencyRate());
			expectDistributorSettlementDetail
					.setParent_distributor_id(parentDistributorId);
			expectDistributorSettlementDetail
					.setOrder_bid_real_pay_amount(distributorSettlement
							.getPayable_amount());
			expectDistributorSettlementDetail
					.setBid_currency(distributorSettlement.getBid_currency());
			expectDistributorSettlementDetail
					.setBid_vs_settlement_exchange_rate(distributorCommisionRateProductBean
							.getBidVsSettlemntExchangeRate());
			expectDistributorSettlementDetail
					.setSettlement_amount(settleAmount);
			expectDistributorSettlementDetail
					.setPay_channel_type(payChannelType);

			if (payChannelType == 5)
				expectDistributorSettlementDetail.setSettlement_currency("MOP");
			else
				expectDistributorSettlementDetail.setSettlement_currency("CNY");

			expectDistributorSettlementDetail
					.setSettlement_status(settlementStatus);
			expectDistributorSettlementDetail
					.setCustomer_payment_order_no(distributorSettlement
							.getCustomer_payment_order_no());
			expectDistributorSettlementDetail
					.setCustomer_payment_order_amount(distributorSettlement
							.getCustomer_payment_order_amount());

			if (settlementWay == 0 && payChannelType != 5)
				expectDistributorSettlementDetail
						.setNeed_exchange(!distributorCommisionRateProductBean
								.getForeignCurrency().equalsIgnoreCase("CNY"));
			else
				expectDistributorSettlementDetail.setNeed_exchange(false);

			expectDistributorSettlementDetail.setShengpay_withdraw_status(0);
			expectDistributorSettlementDetail
					.setForeign_currency(foreignCurrency);
			expectDistributorSettlementDetail
					.setTotal_foreign_amount(foreignAmount);
			expectDistributorSettlementDetail.setSettlement_type(0);
			expectDistributorSettlementDetail
					.setBiz_id(String.valueOf(orderId));
			expectDistributorSettlementDetail
					.setDistributor_type(distributorType);
			expectDistributorSettlementDetail
					.setOrigin_distributor_id(originDistributorId);
			expectDistributorSettlementDetail
					.setOrigin_distributor_name(originDistributorName);
			expectDistributorSettlementDetail
					.setSettlement_way(distributorSettlement
							.getSettlement_way());

			expectDistributorSettlementDetail
					.setFund_not_collect_merchant_id(fundNotCollectMerchantId);

			expectDistributorSettlementDetail.setIs_platform_supplier(isPlatformSupplierOrder);

			DistributorSettlementDetail actualDistributorSettlementDetail = distributorSettlementDetailWrapper
					.selectByPaymentIdAndOrderIdAndDistributorId(paymentId,
							orderId, distributorId);

			this.verifyDistributorSettlementDetailTable(
					expectDistributorSettlementDetail,
					actualDistributorSettlementDetail);

			// 校验资金流水
			if (distributorSettlement.getSettlement_status() == 1
					&& settleAmount.compareTo(BigDecimal.ZERO) == 1
					&& settlementWay == 0 && payChannelType != 5
					&& payChannelType != 61) {

				int accountType = ShenqiSettlementBuildVerifyService.getDistributorAccountType(
						expectDistributorSettlementDetail.getPay_channel_type(),
						expectDistributorSettlementDetail.getSettlement_currency(),
						Utils.zeroIfNull(expectDistributorSettlementDetail
								.getFund_not_collect_merchant_id()) > 0);

				Logger.comment("校验资金账户信息");
				AccountWapper accountWapper = new AccountWapper();
				HashMap<String, Object> accountEntryMap = new HashMap<>();

				accountEntryMap.put("userId", distributorId);
				accountEntryMap.put("bizcode", "360036");
				accountEntryMap.put("accountType", accountType);
				accountEntryMap.put("originalNo", orderId);
				accountEntryMap.put("bizno",
						actualDistributorSettlementDetail.getId());

				DataUtil.verifyDecimalEquals(settleAmount, accountWapper
						.selectAccountEntryByMap(accountEntryMap).get(0)
						.getAmount(), "验证accountEntry日志记录");

				Logger.comment("验证完毕");
			}

			if (this.isSubDistributor(actualDistributorSettlementDetail
					.getDistributor_type()))
				subDistributorSettlementDetail = actualDistributorSettlementDetail;

			if (refundBillPoList != null && settleAmount.compareTo(BigDecimal.ZERO) > 0) {

				BigDecimal refundedDistributorSettleAmount = BigDecimal.ZERO;

				for (RefundBill refundBillPo : refundBillPoList) {
					if (distributorType == 1001 && refunded == 2) {
						Logger.comment("分销机构子分销员，结算后退款，不生成退款结算单");
					} else {

						/**
						 * 如果是子分销员的退款单：子分销员退款金额 = 子分销员得到的分佣金额 × 分销机构退款的金额 / 分销机构得到的分佣金额
						 * 如果是A（分销员/分销机构）的退款单：
						 * 1、仅退款：A退款单金额 = A得到的分佣金额 × 申请的退款金额 / 分销员商品金额
						 * 2、部分退款：A退款单金额 = A得到的分佣金额 × 申请的退款数量 / 分销员商品数量
						 */

						String bizId = refundBillPo.getRefund_no();
						int refundType = refundBillPo.getRefund_type();

						BigDecimal refundProductAmount = refundBillPo
								.getTotal_refund_product_amount();

						DistributorSettlementDetail actualRefundedDistributorSettlementDetail = distributorSettlementDetailWrapper
								.selectRefundedByPaymentIdAndOrderIdAndDistributorIdAndSettlementType(
										paymentId, orderId, distributorId,
										bizId, 1);

						BigDecimal refundSettleAmount = BigDecimal.ZERO;
						BigDecimal refundedProductSettleAmount = distributorCommisionRateProductBean
								.getRefundBillNoRemainsSettleAmountMap().get(
										bizId);
						BigDecimal totalRefundedProductSettlement = distributorCommisionRateProductBean
								.getDistributorProductBeanList()
								.stream()
								.map(DistributorProductBean::getSettlementCurrencySettleAmount)
								.reduce(BigDecimal.ZERO, BigDecimal::add);
						BigDecimal totalProductAmount = distributorCommisionRateProductBean
								.getDistributorProductBeanList()
								.stream()
								.map(distributorProductBean -> distributorProductBean
										.getPerProductPayableAmount()
										.multiply(
												new BigDecimal(
														distributorProductBean
																.getOrderDetailPo()
																.getProductCount())))
								.reduce(BigDecimal.ZERO, BigDecimal::add);

 						Logger.comment(String
								.format("验证distributor_settlement_detail退款单， id：%s；orderId：%s；distributorId：%s；bizId：%s",
										actualRefundedDistributorSettlementDetail
												.getId(), orderId,
										distributorId, bizId));

 						if (refundType == 1) {
							if (this.isSubDistributor(actualDistributorSettlementDetail
									.getDistributor_type())) {

								refundedProductSettleAmount = this
										.calDistributorRefundedAmount(
												refundType, null,
												totalProductAmount,
												refundedProductSettleAmount,
												refundProductAmount).abs();

								refundSettleAmount = this
										.calDistributorRefundedAmount(
												refundType, null,
												totalRefundedProductSettlement,
												settleAmount,
												refundedProductSettleAmount);
							} else {

								refundSettleAmount = this
										.calDistributorRefundedAmount(
												refundType,
												null,
												totalProductAmount,
												refundedProductSettleAmount,
												refundProductAmount);
							}
						} else {

							for (DistributorProductBean distributorProductBean : distributorCommisionRateProductBean
									.getDistributorProductBeanList()) {

								List<RefundProductDetail> refundProductDetails = refundProductDetailWrapper
										.selectByRefundBillNoAndCatalogId(
												bizId, distributorProductBean
														.getOrderDetailPo()
														.getCatalogId());

								if (null == refundProductDetails || refundProductDetails.size() == 0) {
									continue;
								} else {

									RefundProductDetail refundProductDetail = refundProductDetails
											.get(0);

									BigDecimal distributorProductAmount = distributorProductBean
											.getPerProductPayableAmount();
									refundedProductSettleAmount = distributorProductBean
											.getSettlementCurrencySettleAmount()
											.subtract(
													refundedDistributorSettleAmount)
											.multiply(
													new BigDecimal(
															refundProductDetail
																	.getRefund_num()))
											.divide(new BigDecimal(
													refundProductDetail
															.getProduct_num()),
													2, BigDecimal.ROUND_DOWN);

									if (this.isSubDistributor(actualDistributorSettlementDetail
											.getDistributor_type())) {

										refundSettleAmount = settleAmount
												.multiply(
														refundedProductSettleAmount
																.divide(totalRefundedProductSettlement,
																		6,
																		BigDecimal.ROUND_DOWN))
												.setScale(2,
														BigDecimal.ROUND_HALF_UP)
												.negate()
												.add(refundSettleAmount);
									} else {

										refundSettleAmount = this
												.calDistributorRefundedAmount(
														refundType,
														refundProductDetail,
														distributorProductAmount,
														distributorProductBean
																.getSettlementCurrencySettleAmount()
																.subtract(
																		refundedDistributorSettleAmount),
														refundProductAmount)
												.add(refundSettleAmount);
									}
								}
							}
						}

						foreignAmount = BigDecimal.ZERO;

						DataUtil.verifyDecimalEquals(refundSettleAmount,
								actualRefundedDistributorSettlementDetail
										.getSettlement_amount(),
								"验证Settlement_amount");
						DataUtil.verifyDecimalEquals(foreignAmount,
								actualRefundedDistributorSettlementDetail
										.getTotal_foreign_amount(),
								"验证Total_foreign_amount");
						Logger.verifyEquals(bizId,
								actualRefundedDistributorSettlementDetail
										.getBiz_id(), "验证biz_id");

						Logger.comment("验证完毕");

						if (actualDistributorSettlementDetail
								.getSettlement_status() == 4
								|| settleAmount.compareTo(BigDecimal.ZERO) == 0) {

							Logger.comment("无需结算时，没有资金流水");
						} else {

							// 校验资金流水
							if (actualRefundedDistributorSettlementDetail
									.getSettlement_status() == 1
									&& settlementWay == 0 && payChannelType != 5) {

								int accountType = ShenqiSettlementBuildVerifyService.getDistributorAccountType(
										expectDistributorSettlementDetail.getPay_channel_type(),
										expectDistributorSettlementDetail.getSettlement_currency(),
										Utils.zeroIfNull(expectDistributorSettlementDetail
												.getFund_not_collect_merchant_id()) > 0);

								Logger.comment("校验退款时的资金账户信息");
								AccountWapper accountWapper = new AccountWapper();
								HashMap<String, Object> accountEntryMap = new HashMap<>();

								accountEntryMap.put("userId", distributorId);
								accountEntryMap.put("bizcode", "360036");
								accountEntryMap.put("accountType", accountType);
								accountEntryMap.put("originalNo", orderId);
								accountEntryMap.put("bizno",
										actualRefundedDistributorSettlementDetail
												.getId());

								DataUtil.verifyDecimalEquals(
										refundSettleAmount,
										accountWapper
												.selectAccountEntryByMap(
														accountEntryMap).get(0)
												.getAmount(),
										"验证accountEntry日志记录");

								Logger.comment("验证完毕");
							}
						}
					}
				}
			}

			// 如果不是分销机构/CPS的子分销员，校验distributor_product_detail表数据
			if (!this.isSubDistributor(distributorType)) {

				this.buildDistributorProductDetailForVerify(
						distributorCommisionRateProductBean,
						actualDistributorSettlementDetail, 0);

				if (null != subDistributorSettlementDetail
						&& subDistributorSettlementDetail
								.getSettlement_amount().compareTo(
										BigDecimal.ZERO) != 0)
					this.buildAgencyToSubDistributorSettlementDetailForVerify(
							distributorCommisionRateProductBean,
							actualDistributorSettlementDetail,
							subDistributorSettlementDetail, refundBillPoList, refunded);
			}
		}
	}

	/**
	 * 合伙人
	 *
	 * @param distributorSettlement
	 * @param delayDays
	 * @param partnerCommisionRateProductBeanList
	 * @param refundBillPoList
	 * @throws CloneNotSupportedException
	 */
	public void buildPartnerDistributorSettlementDetailForVerify(
			DistributorSettlement distributorSettlement,
			int delayDays,
			ArrayList<DistributorCommisionRateProductBean> partnerCommisionRateProductBeanList,
			List<RefundBill> refundBillPoList, Long fundNotCollectMerchantId, Boolean isPlatformSupplierOrder)
			throws CloneNotSupportedException {

		Logger.comment("校验合伙人DistributorSettlementDetail");

		for (DistributorCommisionRateProductBean partnerCommisionRateProductBean : partnerCommisionRateProductBeanList) {

			DistributorRelation distributorRelation = partnerCommisionRateProductBean
					.getDistributorRelation();

			PartnerRewardRate partnerRewardRate = null;

			Integer partnerDistributionConfigId = null;
			String partnerDistributorLevelLabel = null;
			Integer partnerDistributorLevel = null;

			Long parentDistributorId = null;
			Long originDistributorId = partnerCommisionRateProductBean
					.getOriginDistributorId();
			String originDistributorName = partnerCommisionRateProductBean
					.getOriginDistributorName();

			// 普通分销员
			partnerRewardRate = partnerCommisionRateProductBean
					.getPartnerRewardRate();

			partnerDistributionConfigId = partnerRewardRate
					.getPartner_config_id().intValue();
			partnerDistributorLevelLabel = partnerRewardRate
					.getPartner_level_label();
			partnerDistributorLevel = partnerRewardRate
					.getPartner_level_index();

			parentDistributorId = distributorRelation
					.getParent_distributor_id();

			Long orderId = distributorSettlement.getOrder_id();
			Long paymentId = distributorSettlement.getPayment_id();

			int payChannelType = partnerCommisionRateProductBean
					.getPayChannelType();
			Long distributorId = distributorRelation.getDistributor_id();

			BigDecimal settleAmount = partnerCommisionRateProductBean
					.getDistributorSettlementCurrencySettleAmount();
			String foreignCurrency = partnerCommisionRateProductBean
					.getForeignCurrency();
			Integer settlementWay = Utils.zeroIfNull(distributorSettlement
					.getSettlement_way());

			BigDecimal foreignAmount = BigDecimal.ZERO;

			DistributorSettlementDetail expectDistributorSettlementDetail = new DistributorSettlementDetail();

			expectDistributorSettlementDetail.setOrder_id(orderId);
			expectDistributorSettlementDetail.setPayment_id(paymentId);
			expectDistributorSettlementDetail.setUser_id(distributorSettlement
					.getUser_id());
			expectDistributorSettlementDetail
					.setSeller_id(distributorSettlement.getSeller_id());
			expectDistributorSettlementDetail.setDistributor_id(distributorId);
			expectDistributorSettlementDetail
					.setDistribution_config_id(partnerDistributionConfigId);
			expectDistributorSettlementDetail
					.setDistributor_name(distributorRelation
							.getDistributor_name());
			expectDistributorSettlementDetail
					.setDistributor_level(partnerDistributorLevel);
			expectDistributorSettlementDetail
					.setDistributor_level_label(partnerDistributorLevelLabel);
			expectDistributorSettlementDetail
					.setDistributor_phone(distributorRelation
							.getDistributor_phone());
			expectDistributorSettlementDetail
					.setDistributor_rate(partnerCommisionRateProductBean
							.getCurrencyRate());
			expectDistributorSettlementDetail
					.setParent_distributor_id(parentDistributorId);
			expectDistributorSettlementDetail
					.setOrder_bid_real_pay_amount(distributorSettlement
							.getPayable_amount());
			expectDistributorSettlementDetail
					.setBid_currency(distributorSettlement.getBid_currency());
			expectDistributorSettlementDetail
					.setBid_vs_settlement_exchange_rate(partnerCommisionRateProductBean
							.getBidVsSettlemntExchangeRate());
			expectDistributorSettlementDetail
					.setSettlement_amount(settleAmount);

			if (payChannelType == 5)
				expectDistributorSettlementDetail.setSettlement_currency("MOP");
			else
				expectDistributorSettlementDetail.setSettlement_currency("CNY");

			if (settleAmount.compareTo(BigDecimal.ZERO) == 0)
				expectDistributorSettlementDetail.setSettlement_status(2);
			else
				expectDistributorSettlementDetail
						.setSettlement_status(distributorSettlement
								.getSettlement_status());

			expectDistributorSettlementDetail
					.setPay_channel_type(payChannelType);
			expectDistributorSettlementDetail
					.setCustomer_payment_order_no(distributorSettlement
							.getCustomer_payment_order_no());
			expectDistributorSettlementDetail
					.setCustomer_payment_order_amount(distributorSettlement
							.getCustomer_payment_order_amount());

			if (settlementWay == 0 && payChannelType != 5)
				expectDistributorSettlementDetail
						.setNeed_exchange(partnerCommisionRateProductBean
								.getForeignCurrency().equalsIgnoreCase("CNY") ? false
								: true);
			else
				expectDistributorSettlementDetail.setNeed_exchange(false);

			expectDistributorSettlementDetail.setShengpay_withdraw_status(0);
			expectDistributorSettlementDetail
					.setForeign_currency(foreignCurrency);
			expectDistributorSettlementDetail
					.setTotal_foreign_amount(foreignAmount);
			expectDistributorSettlementDetail.setSettlement_type(4);
			expectDistributorSettlementDetail
					.setBiz_id(String.valueOf(orderId));
			expectDistributorSettlementDetail
					.setDistributor_type(distributorRelation
							.getDistributor_type());
			expectDistributorSettlementDetail
					.setOrigin_distributor_id(originDistributorId);
			expectDistributorSettlementDetail
					.setOrigin_distributor_name(originDistributorName);
			expectDistributorSettlementDetail
					.setSettlement_way(distributorSettlement
							.getSettlement_way());
			expectDistributorSettlementDetail
					.setFund_not_collect_merchant_id(fundNotCollectMerchantId);
			expectDistributorSettlementDetail.setIs_platform_supplier(isPlatformSupplierOrder);

			DistributorSettlementDetail actualDistributorSettlementDetail = distributorSettlementDetailWrapper
					.selectByPaymentIdAndOrderIdAndDistributorIdAndSettlementType(
							paymentId, orderId, distributorId, 4);

			this.verifyDistributorSettlementDetailTable(
					expectDistributorSettlementDetail,
					actualDistributorSettlementDetail);

			// 校验资金流水
			if (distributorSettlement.getSettlement_status() == 1
					&& settleAmount.compareTo(BigDecimal.ZERO) == 1
					&& settlementWay == 0 && payChannelType != 5) {

				int accountType = ShenqiSettlementBuildVerifyService
						.getDistributorAccountType(
								actualDistributorSettlementDetail
										.getPay_channel_type(),
								actualDistributorSettlementDetail
										.getSettlement_currency(),
								Utils.zeroIfNull(actualDistributorSettlementDetail
										.getFund_not_collect_merchant_id()) > 0 ? true
										: false);

				Logger.comment("校验资金账户信息");
				AccountWapper accountWapper = new AccountWapper();
				HashMap<String, Object> accountEntryMap = new HashMap<>();

				accountEntryMap.put("userId", distributorId);
				accountEntryMap.put("bizcode", "370038");
				accountEntryMap.put("accountType", accountType);
				accountEntryMap.put("originalNo", orderId);
				accountEntryMap.put("bizno",
						actualDistributorSettlementDetail.getId());

				DataUtil.verifyDecimalEquals(settleAmount, accountWapper
						.selectAccountEntryByMap(accountEntryMap).get(0)
						.getAmount(), "验证accountEntry日志记录");
			}

			if (refundBillPoList != null) {

				for (RefundBill refundBillPo : refundBillPoList) {

					// String refundedCatalogId = refundBillPo.getCatalogId();
					BigDecimal refundedProductAmount = refundBillPo
							.getTotal_refund_product_amount();
					String bizId = refundBillPo.getRefund_no();

					DistributorSettlementDetail actualRefundedDistributorSettlementDetail = distributorSettlementDetailWrapper
							.selectRefundedByPaymentIdAndOrderIdAndDistributorIdAndSettlementType(
									paymentId, orderId, distributorId, bizId, 5);

					BigDecimal refundedProductSettlement = partnerCommisionRateProductBean
							.getDistributorProductBeanList()
							.stream()
							.map(DistributorProductBean::getSettlementCurrencySettleAmount)
							.reduce(BigDecimal.ZERO, BigDecimal::add);

					BigDecimal distributorProductAmount = partnerCommisionRateProductBean
							.getDistributorProductBeanList()
							.stream()
							.map(DistributorProductBean::getProductPayableAmount)
							.reduce(BigDecimal.ZERO, BigDecimal::add);
					BigDecimal refundSettleAmount = BigDecimal.ZERO;

					Logger.comment(String
							.format("验证合伙人distributor_settlement_detail退款单， orderId：%s；distributorId：%s；bizId：%s",
									orderId, distributorId, bizId));

					refundSettleAmount = refundedProductSettlement
							.multiply(
									refundedProductAmount.divide(
											distributorProductAmount, 6,
											BigDecimal.ROUND_HALF_UP))
							.setScale(2, BigDecimal.ROUND_DOWN).negate();

					foreignAmount = BigDecimal.ZERO;

					DataUtil.verifyDecimalEquals(refundSettleAmount,
							actualRefundedDistributorSettlementDetail
									.getSettlement_amount(),
							"验证Settlement_amount");
					DataUtil.verifyDecimalEquals(foreignAmount,
							actualRefundedDistributorSettlementDetail
									.getTotal_foreign_amount(),
							"验证Total_foreign_amount");
					Logger.verifyEquals(bizId,
							actualRefundedDistributorSettlementDetail
									.getBiz_id(), "验证biz_id");

					if (actualDistributorSettlementDetail
							.getSettlement_status() == 4
							|| settleAmount.compareTo(BigDecimal.ZERO) == 0) {

						Logger.comment("无需结算时，没有资金流水");
					} else {
						// 校验资金流水
						if (actualRefundedDistributorSettlementDetail
								.getSettlement_status() == 1
								&& settlementWay == 0) {

							int accountType = ShenqiSettlementBuildVerifyService
									.getDistributorAccountType(
											actualDistributorSettlementDetail
													.getPay_channel_type(),
											actualDistributorSettlementDetail
													.getSettlement_currency(),
											Utils.zeroIfNull(actualDistributorSettlementDetail
													.getFund_not_collect_merchant_id()) > 0);

							Logger.comment("校验退款时的资金账户信息");
							AccountWapper accountWapper = new AccountWapper();
							HashMap<String, Object> accountEntryMap = new HashMap<>();

							accountEntryMap.put("userId", distributorId);
							accountEntryMap.put("bizcode", "370038");
							accountEntryMap.put("accountType", accountType);
							accountEntryMap.put("originalNo", orderId);
							accountEntryMap.put("bizno",
									actualRefundedDistributorSettlementDetail
											.getId());

							DataUtil.verifyDecimalEquals(
									refundSettleAmount,
									accountWapper
											.selectAccountEntryByMap(
													accountEntryMap).get(0)
											.getAmount(), "验证accountEntry日志记录");
						}
					}
				}
			}

			// 如果不是分销机构/CPS的子分销员，校验distributor_product_detail表数据
			if (!this.isSubDistributor(actualDistributorSettlementDetail
					.getDistributor_type())) {

				this.buildDistributorProductDetailForVerify(
						partnerCommisionRateProductBean,
						actualDistributorSettlementDetail, 1);
			}
		}
	}

	public void buildDistributorProductDetailForVerify(
			DistributorCommisionRateProductBean distributorCommisionRateProductBean,
			DistributorSettlementDetail distributorSettlementDetail,
			Integer partnerType) {

		for (DistributorProductBean distributorProductBean : distributorCommisionRateProductBean
				.getDistributorProductBeanList()) {

			DistributorProductDetail expectDistributorProductDetail = new DistributorProductDetail();

			OrderDetailPo orderDetailPo = distributorProductBean
					.getOrderDetailPo();

			BigDecimal distributorRate = BigDecimal.ZERO;

			if (null != distributorProductBean.getProductCommissionRate()) {

				if (null != distributorProductBean.getProductCurrencyRate())
					distributorRate = distributorProductBean
							.getProductCurrencyRate();
				else
					distributorRate = distributorSettlementDetail
							.getDistributor_rate();

			} else
				distributorRate = distributorSettlementDetail
						.getDistributor_rate();

			long orderId = distributorSettlementDetail.getOrder_id();
			long distributorId = distributorSettlementDetail
					.getDistributor_id();
			String catalogId = orderDetailPo.getCatalogId();

			expectDistributorProductDetail.setOrder_id(orderId);
			expectDistributorProductDetail.setDistributor_id(distributorId);
			expectDistributorProductDetail.setDistributor_rate(distributorRate);
			expectDistributorProductDetail.setProduct_id(orderDetailPo
					.getProductId());
			expectDistributorProductDetail.setCatalog_id(catalogId);
			expectDistributorProductDetail.setProduct_num(orderDetailPo
					.getProductCount());
			expectDistributorProductDetail.setProduct_price(orderDetailPo
					.getProductPrice());
			expectDistributorProductDetail
					.setBid_currency(distributorSettlementDetail
							.getBid_currency());
			expectDistributorProductDetail
					.setSettlement_currency(distributorSettlementDetail
							.getSettlement_currency());
			expectDistributorProductDetail
					.setPer_product_freight_amount(distributorProductBean
							.getPerProductFreightAmount());
			expectDistributorProductDetail
					.setPer_product_payable_amount(distributorProductBean
							.getPerProductPayableAmount());
			expectDistributorProductDetail
					.setPer_product_tax_amount(distributorProductBean
							.getPerProductTaxAmount());
			expectDistributorProductDetail
					.setProduct_settle_amount(distributorProductBean
							.getSettlementCurrencySettleAmount());
			expectDistributorProductDetail.setPartner_type(partnerType);

			expectDistributorProductDetail
					.setProduct_config_id(distributorProductBean
							.getProductConfigId() == null ? null
							: distributorProductBean.getProductConfigId()
									.intValue());

			expectDistributorProductDetail
					.setProduct_bid_refund_amount(partnerType == 1 ? BigDecimal.ZERO
							: distributorProductBean
									.getRefundedProductBidAmount());
			expectDistributorProductDetail
					.setProduct_settle_refund_amount(partnerType == 1 ? BigDecimal.ZERO
							: distributorProductBean
									.getRefundedProductSettleAmount());

			DistributorProductDetail actualDistributorProductDetail = distributorProductDetailWrapper
					.selectByOrderIdAndDistributorIdAndCatalogId(orderId,
							distributorId, catalogId, partnerType);

			this.verifyDistributorProductDetailTable(
					expectDistributorProductDetail,
					actualDistributorProductDetail);
		}
	}

	/**
	 * 构建分销机构/CPS给子分销员的结算明细
	 *
	 * @param agencyDistributorSettlementDetail
	 *            分销机构/CPS的结算明细
	 * @param subDistributorSettlementDetail
	 *            子分销员
	 * @throws CloneNotSupportedException
	 */
	public void buildAgencyToSubDistributorSettlementDetailForVerify(
			DistributorCommisionRateProductBean distributorCommisionRateProductBean,
			DistributorSettlementDetail agencyDistributorSettlementDetail,
			DistributorSettlementDetail subDistributorSettlementDetail,
			List<RefundBill> refundBillPoList, int refunded)
			throws CloneNotSupportedException {

		Logger.comment("构建分销机构/CPS给子分销员的结算明细");

		DistributorSettlementDetail expectDistributorSettlementDetail = agencyDistributorSettlementDetail
				.clone();

		Long distributorId = expectDistributorSettlementDetail
				.getDistributor_id();
		Integer payChannelType = expectDistributorSettlementDetail
				.getPay_channel_type();
		Long orderId = expectDistributorSettlementDetail.getOrder_id();
		Long paymentId = expectDistributorSettlementDetail.getPayment_id();

		BigDecimal settleAmount = subDistributorSettlementDetail
				.getSettlement_amount().negate();

		expectDistributorSettlementDetail
				.setDistributor_rate(subDistributorSettlementDetail
						.getDistributor_rate());
		expectDistributorSettlementDetail.setSettlement_amount(settleAmount);
		expectDistributorSettlementDetail.setSettlement_type(2);

		DistributorSettlementDetail actualDistributorSettlementDetail = distributorSettlementDetailWrapper
				.selectByPaymentIdAndOrderIdAndDistributorIdAndSettlementType(
						paymentId, orderId, distributorId, 2);

		this.verifyDistributorSettlementDetailTable(
				expectDistributorSettlementDetail,
				actualDistributorSettlementDetail);

		int settlementWay = Utils.zeroIfNull(expectDistributorSettlementDetail
				.getSettlement_way());

		// 校验资金流水
		if (expectDistributorSettlementDetail.getSettlement_status() == 1
				&& settlementWay == 0
				&& expectDistributorSettlementDetail.getPay_channel_type() != 5
				&& expectDistributorSettlementDetail.getPay_channel_type() != 61) {

			int accountType = ShenqiSettlementBuildVerifyService
					.getDistributorAccountType(
							actualDistributorSettlementDetail
									.getPay_channel_type(),
							actualDistributorSettlementDetail
									.getSettlement_currency(),
							Utils.zeroIfNull(actualDistributorSettlementDetail
									.getFund_not_collect_merchant_id()) > 0 ? true
									: false);

			Logger.comment("校验资金账户信息");
			AccountWapper accountWapper = new AccountWapper();
			HashMap<String, Object> accountEntryMap = new HashMap<>();

			accountEntryMap.put("userId", distributorId);
			accountEntryMap.put("bizcode", "360036");
			accountEntryMap.put("accountType", accountType);
			accountEntryMap.put("originalNo", orderId);
			accountEntryMap.put("bizno",
					actualDistributorSettlementDetail.getId());

			DataUtil.verifyDecimalEquals(settleAmount, accountWapper
					.selectAccountEntryByMap(accountEntryMap).get(0)
					.getAmount(), "验证accountEntry日志记录");
		}

		if (refundBillPoList != null && refunded != 2) {

			for (RefundBill refundBillPo : refundBillPoList) {

				String bizId = refundBillPo.getRefund_no();
				Integer refundType = refundBillPo.getRefund_type();
				BigDecimal refundProductAmount = refundBillPo
						.getTotal_refund_product_amount();
				BigDecimal refundedProductSettleAmount = distributorCommisionRateProductBean
						.getRefundBillNoRemainsSettleAmountMap().get(bizId);

				DistributorSettlementDetail actualRefundedDistributorSettlementDetail = distributorSettlementDetailWrapper
						.selectRefundedByPaymentIdAndOrderIdAndDistributorIdAndSettlementType(
								paymentId, orderId, distributorId, bizId, 3);

				BigDecimal totalRefundedProductSettlement = distributorCommisionRateProductBean
						.getDistributorProductBeanList()
						.stream()
						.map(DistributorProductBean::getSettlementCurrencySettleAmount)
						.reduce(BigDecimal.ZERO, BigDecimal::add);
				BigDecimal totalProductAmount = distributorCommisionRateProductBean
						.getDistributorProductBeanList()
						.stream()
						.map(distributorProductBean -> distributorProductBean
								.getPerProductPayableAmount().multiply(
										new BigDecimal(distributorProductBean
												.getOrderDetailPo()
												.getProductCount())))
						.reduce(BigDecimal.ZERO, BigDecimal::add);

				BigDecimal refundSettleAmount = BigDecimal.ZERO;

				Logger.comment(String
						.format("验证分销机构子分销员distributor_settlement_detail退款单，id：%s， orderId：%s；distributorId：%s；bizId：%s；settlementType：%s",
								actualRefundedDistributorSettlementDetail
										.getId(), orderId, distributorId,
								bizId,
								actualRefundedDistributorSettlementDetail
										.getSettlement_type()));

				if (actualRefundedDistributorSettlementDetail
						.getId() == 10130)
					System.out.print(10130);

				if (refundType == 1) {

					refundedProductSettleAmount = this
							.calDistributorRefundedAmount(refundType, null,
									totalProductAmount,
									refundedProductSettleAmount,
									refundProductAmount).abs();

					refundSettleAmount = this.calDistributorRefundedAmount(
							refundType, null, totalRefundedProductSettlement,
							settleAmount, refundedProductSettleAmount);
				} else {

					BigDecimal refundedDistributorSettleAmount = BigDecimal.ZERO;

					for (DistributorProductBean distributorProductBean : distributorCommisionRateProductBean
							.getDistributorProductBeanList()) {

						refundedDistributorSettleAmount = distributorProductBean
								.getRefundedSettleAmountMap().get(bizId)
								.add(refundedDistributorSettleAmount);
					}

					refundSettleAmount = this.calDistributorRefundedAmount(
							1, null,
							totalRefundedProductSettlement,
							settleAmount, refundedDistributorSettleAmount).abs();
				}

				DataUtil.verifyDecimalEquals(refundSettleAmount,
						actualRefundedDistributorSettlementDetail
								.getSettlement_amount(),
						"验证Settlement_amount");
				DataUtil.verifyDecimalEquals(BigDecimal.ZERO,
						actualRefundedDistributorSettlementDetail
								.getTotal_foreign_amount(),
						"验证Total_foreign_amount");
				Logger.verifyEquals(bizId,
						actualRefundedDistributorSettlementDetail
								.getBiz_id(), "验证biz_id");

				if (actualDistributorSettlementDetail.getSettlement_status() == 4
						|| settleAmount.compareTo(BigDecimal.ZERO) == 0) {

					Logger.comment("无需结算时，没有资金流水");
				} else {

					// 校验资金流水
					if (actualRefundedDistributorSettlementDetail
							.getSettlement_status() == 1 && settlementWay == 0 && payChannelType != 5) {

						int accountType = ShenqiSettlementBuildVerifyService
								.getDistributorAccountType(
										actualDistributorSettlementDetail
												.getPay_channel_type(),
										actualDistributorSettlementDetail
												.getSettlement_currency(),
										Utils.zeroIfNull(actualDistributorSettlementDetail
												.getFund_not_collect_merchant_id()) > 0 ? true
												: false);

						Logger.comment("校验退款时的资金账户信息");
						AccountWapper accountWapper = new AccountWapper();
						HashMap<String, Object> accountEntryMap = new HashMap<>();

						accountEntryMap.put("userId", distributorId);
						accountEntryMap.put("bizcode", "360036");
						accountEntryMap.put("accountType", accountType);
						accountEntryMap.put("originalNo", orderId);
						accountEntryMap.put("bizno",
								actualRefundedDistributorSettlementDetail
										.getId());

						DataUtil.verifyDecimalEquals(
								refundSettleAmount,
								accountWapper
										.selectAccountEntryByMap(
												accountEntryMap).get(0)
										.getAmount(), "验证accountEntry日志记录");
					}
				}
			}
		}
	}

	public void verifyDistributorSettlementTable(
			DistributorSettlement expectDistributorSettlement,
			DistributorSettlement actualDistributorSettlement,
			int settlementDelayDays, Boolean settled) {

		Logger.comment(String
				.format("验证distributor_settlement， paymentId：%s；orderId：%s，distributorType：%s",
						actualDistributorSettlement.getPayment_id(),
						actualDistributorSettlement.getOrder_id(),
						actualDistributorSettlement.getDistributor_type()));

		Logger.verifyEquals(expectDistributorSettlement.getOrder_id(),
				actualDistributorSettlement.getOrder_id(), "验证Order_id");
		Logger.verifyEquals(expectDistributorSettlement.getPayment_id(),
				actualDistributorSettlement.getPayment_id(), "验证Payment_id");
		Logger.verifyEquals(expectDistributorSettlement.getUser_id(),
				actualDistributorSettlement.getUser_id(), "验证User_id");
		Logger.verifyEquals(expectDistributorSettlement.getSeller_id(),
				actualDistributorSettlement.getSeller_id(), "验证Seller_id");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getPayable_amount(),
				actualDistributorSettlement.getPayable_amount(),
				"验证Payable_amount");
		Logger.verifyEquals(expectDistributorSettlement.getBid_currency(),
				actualDistributorSettlement.getBid_currency(), "验证Bid_currency");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getCommission_amount(),
				actualDistributorSettlement.getCommission_amount(),
				"验证Commission_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getBid_commission_amount(),
				actualDistributorSettlement.getBid_commission_amount(),
				"验证Bid_commission_amount");
		Logger.verifyEquals(
				expectDistributorSettlement.getDirect_distributor_id(),
				actualDistributorSettlement.getDirect_distributor_id(),
				"验证Direct_distributor_id");
		Logger.verifyEquals(
				expectDistributorSettlement.getDirect_distributor_name(),
				actualDistributorSettlement.getDirect_distributor_name(),
				"验证Direct_distributor_name");
		Logger.verifyEquals(expectDistributorSettlement.getSettlement_status(),
				actualDistributorSettlement.getSettlement_status(),
				"验证Settlement_status");
		Logger.verifyEquals(expectDistributorSettlement.getHp_settled(),
				actualDistributorSettlement.getHp_settled(), "验证Hp_settled");
		Logger.verifyEquals(
				expectDistributorSettlement.getCustomer_payment_order_no(),
				actualDistributorSettlement.getCustomer_payment_order_no(),
				"验证Customer_payment_order_no");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getCustomer_payment_order_amount(),
				actualDistributorSettlement.getCustomer_payment_order_amount(),
				"验证Customer_payment_order_amount");

		if (!settled
				|| actualDistributorSettlement.getSettlement_status() == 4
				|| (Utils.zeroIfNull(
						actualDistributorSettlement.getBonus_amount())
						.compareTo(BigDecimal.ZERO) == 0 && Utils.zeroIfNull(
						actualDistributorSettlement.getCommission_amount())
						.compareTo(BigDecimal.ZERO) == 0))
			Logger.verifyEquals(null,
					actualDistributorSettlement.getExpected_settlement_time(),
					"验证Expected_settlement_time");
		else
			Logger.verifyEquals(settlementDelayDays, Utils.daysBetween(
					YMTDateUtil.getDate(),
					actualDistributorSettlement.getExpected_settlement_time()),
					"验证Expected_settlement_time");

		Logger.verifyEquals(
				expectDistributorSettlement.getSettlement_result(),
				actualDistributorSettlement.getSettlement_result(),
				"验证Settlement_result");

		// 20200924
		Logger.verifyEquals(expectDistributorSettlement.getDistributor_type(),
				actualDistributorSettlement.getDistributor_type(),
				"验证Distributor_type");

		Logger.verifyEquals(expectDistributorSettlement.getSettlement_way(),
				actualDistributorSettlement.getSettlement_way(),
				"验证Settlement_way");

		// 20201209
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getBonus_amount(),
				actualDistributorSettlement.getBonus_amount(), "验证Bonus_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlement.getBid_bonus_amount(),
				actualDistributorSettlement.getBid_bonus_amount(),
				"验证Bid_bonus_amount");
	}

	public void verifyDistributorSettlementDetailTable(
			DistributorSettlementDetail expectDistributorSettlementDetail,
			DistributorSettlementDetail actualDistributorSettlementDetail) {

		Logger.comment(String
				.format("验证distributor_settlement_detail，id：%s， orderId：%s；distributorId：%s；distributorType：%s；settlementType：%s",
						actualDistributorSettlementDetail.getId(),
						actualDistributorSettlementDetail.getOrder_id(),
						actualDistributorSettlementDetail.getDistributor_id(),
						actualDistributorSettlementDetail.getDistributor_type(),
						actualDistributorSettlementDetail.getSettlement_type()));

		Logger.verifyEquals(expectDistributorSettlementDetail.getOrder_id(),
				actualDistributorSettlementDetail.getOrder_id(), "验证Order_id");
		Logger.verifyEquals(expectDistributorSettlementDetail.getPayment_id(),
				actualDistributorSettlementDetail.getPayment_id(),
				"验证Payment_id");
		Logger.verifyEquals(expectDistributorSettlementDetail.getUser_id(),
				actualDistributorSettlementDetail.getUser_id(), "验证User_id");
		Logger.verifyEquals(expectDistributorSettlementDetail.getSeller_id(),
				actualDistributorSettlementDetail.getSeller_id(), "验证Seller_id");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_id(),
				actualDistributorSettlementDetail.getDistributor_id(),
				"验证Distributor_id");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistribution_config_id(),
				actualDistributorSettlementDetail.getDistribution_config_id(),
				"验证Distribution_config_id");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_name(),
				actualDistributorSettlementDetail.getDistributor_name(),
				"验证Distributor_name");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_level(),
				actualDistributorSettlementDetail.getDistributor_level(),
				"验证Distributor_level");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_level_label(),
				actualDistributorSettlementDetail.getDistributor_level_label(),
				"验证Distributor_level_label");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_phone(),
				actualDistributorSettlementDetail.getDistributor_phone(),
				"验证Distributor_phone");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlementDetail.getDistributor_rate(),
				actualDistributorSettlementDetail.getDistributor_rate(),
				"验证Distributor_rate");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getParent_distributor_id(),
				actualDistributorSettlementDetail.getParent_distributor_id(),
				"验证Parent_distributor_id");
		DataUtil.verifyDecimalEquals(expectDistributorSettlementDetail
				.getOrder_bid_real_pay_amount(),
				expectDistributorSettlementDetail
						.getOrder_bid_real_pay_amount(),
				"验证Order_bid_real_pay_amount");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getBid_currency(),
				actualDistributorSettlementDetail.getBid_currency(),
				"验证Bid_currency");
		DataUtil.verifyDecimalEquals(expectDistributorSettlementDetail
				.getBid_vs_settlement_exchange_rate(),
				actualDistributorSettlementDetail
						.getBid_vs_settlement_exchange_rate(),
				"验证Bid_vs_settlement_exchange_rate");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getSettlement_currency(),
				actualDistributorSettlementDetail.getSettlement_currency(),
				"验证Settlement_currency");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlementDetail.getSettlement_amount(),
				actualDistributorSettlementDetail.getSettlement_amount(),
				"验证Settlement_amount");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getSettlement_status(),
				actualDistributorSettlementDetail.getSettlement_status(),
				"验证Settlement_status");
		DataUtil.verifyDateEquals(
				expectDistributorSettlementDetail.getSettlement_time(),
				expectDistributorSettlementDetail.getSettlement_time(),
				"验证Settlement_time");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getPay_channel_type(),
				actualDistributorSettlementDetail.getPay_channel_type(),
				"验证Pay_channel_type");
		Logger.verifyEquals(expectDistributorSettlementDetail
				.getCustomer_payment_order_no(),
				actualDistributorSettlementDetail
						.getCustomer_payment_order_no(),
				"验证Customer_payment_order_no");
		DataUtil.verifyDecimalEquals(expectDistributorSettlementDetail
				.getCustomer_payment_order_amount(),
				expectDistributorSettlementDetail
						.getCustomer_payment_order_amount(),
				"验证Customer_payment_order_amount");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getNeed_exchange(),
				actualDistributorSettlementDetail.getNeed_exchange(),
				"验证Need_exchange");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getShengpay_withdraw_status(),
				actualDistributorSettlementDetail.getShengpay_withdraw_status(),
				"验证Shengpay_withdraw_status");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getForeign_currency(),
				actualDistributorSettlementDetail.getForeign_currency(),
				"验证Foreign_currency");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlementDetail.getTotal_foreign_amount(),
				actualDistributorSettlementDetail.getTotal_foreign_amount(),
				"验证Total_foreign_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorSettlementDetail.getSettlement_type(),
				actualDistributorSettlementDetail.getSettlement_type(),
				"验证Settlement_type");

		// 20200810
		Logger.verifyEquals(expectDistributorSettlementDetail.getBiz_id(),
				actualDistributorSettlementDetail.getBiz_id(), "验证Biz_id");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getDistributor_type(),
				actualDistributorSettlementDetail.getDistributor_type(),
				"验证Distributor_type");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getOrigin_distributor_id(),
				actualDistributorSettlementDetail.getOrigin_distributor_id(),
				"验证Origin_distributor_id");
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getOrigin_distributor_name(),
				actualDistributorSettlementDetail.getOrigin_distributor_name(),
				"验证Origin_distributor_name");

		// 20200924
		Logger.verifyEquals(
				expectDistributorSettlementDetail.getSettlement_way(),
				actualDistributorSettlementDetail.getSettlement_way(),
				"验证Settlement_way");

		// 20210130
		Logger.verifyEquals(expectDistributorSettlementDetail
				.getFund_not_collect_merchant_id(),
				actualDistributorSettlementDetail
						.getFund_not_collect_merchant_id(),
				"验证Fund_not_collect_merchant_id");

		// 20211111
		Logger.verifyEquals(expectDistributorSettlementDetail
				.getIs_platform_supplier(),
				actualDistributorSettlementDetail
						.getIs_platform_supplier(),
				"验证Is_platform_supplier");

		Logger.comment("验证完成");
	}

	public void verifyDistributorProductDetailTable(
			DistributorProductDetail expectDistributorProductDetail,
			DistributorProductDetail actualDistributorProductDetail) {

		Logger.comment(String
				.format("验证distributor_product_detail， distributorId：%s；catalogid：%s；partnerType：%s",
						actualDistributorProductDetail.getDistributor_id(),
						actualDistributorProductDetail.getCatalog_id(),
						actualDistributorProductDetail.getPartner_type()));

		Logger.verifyEquals(expectDistributorProductDetail.getOrder_id(),
				actualDistributorProductDetail.getOrder_id(), "验证Order_id");
		Logger.verifyEquals(expectDistributorProductDetail.getDistributor_id(),
				actualDistributorProductDetail.getDistributor_id(),
				"验证Distributor_id");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getDistributor_rate(),
				actualDistributorProductDetail.getDistributor_rate(),
				"验证Distributor_rate");
		Logger.verifyEquals(expectDistributorProductDetail.getProduct_id(),
				actualDistributorProductDetail.getProduct_id(), "验证Product_id");
		Logger.verifyEquals(expectDistributorProductDetail.getCatalog_id(),
				actualDistributorProductDetail.getCatalog_id(), "验证Catalog_id");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getProduct_price(),
				actualDistributorProductDetail.getProduct_price(),
				"验证Product_price");
		Logger.verifyEquals(expectDistributorProductDetail.getProduct_num(),
				actualDistributorProductDetail.getProduct_num(),
				"验证Product_num");
		Logger.verifyEquals(expectDistributorProductDetail.getBid_currency(),
				actualDistributorProductDetail.getBid_currency(),
				"验证Bid_currency");
		Logger.verifyEquals(
				expectDistributorProductDetail.getSettlement_currency(),
				actualDistributorProductDetail.getSettlement_currency(),
				"验证Settlement_currency");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getPer_product_payable_amount(),
				actualDistributorProductDetail.getPer_product_payable_amount(),
				"验证Per_product_payable_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getPer_product_freight_amount(),
				actualDistributorProductDetail.getPer_product_freight_amount(),
				"验证Per_product_freight_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getPer_product_tax_amount(),
				actualDistributorProductDetail.getPer_product_tax_amount(),
				"验证Per_product_tax_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getProduct_settle_amount(),
				actualDistributorProductDetail.getProduct_settle_amount(),
				"验证Product_settle_amount");

		Logger.verifyEquals(expectDistributorProductDetail.getPartner_type(),
				actualDistributorProductDetail.getPartner_type(),
				"验证Partner_type");

		Logger.verifyEquals(
				expectDistributorProductDetail.getProduct_config_id(),
				actualDistributorProductDetail.getProduct_config_id(),
				"验证Product_config_id");

		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getProduct_bid_refund_amount(),
				actualDistributorProductDetail.getProduct_bid_refund_amount(),
				"验证Refund_product_bid_amount");
		DataUtil.verifyDecimalEquals(
				expectDistributorProductDetail.getProduct_settle_refund_amount(),
				actualDistributorProductDetail.getProduct_settle_refund_amount(),
				"验证Refund_product_settle_amount");
	}

	/**
	 * 计算分销员退款结算金额：1按比例退金额；2带商品退款按数量退金额
	 * @param type
	 */
	private BigDecimal calDistributorRefundedAmount(int type,
			RefundProductDetail refundProductDetail, BigDecimal distributorProductAmount,
			BigDecimal settleAmount, BigDecimal refundedProductAmount) {

		BigDecimal refundSettleAmount = BigDecimal.ZERO;

		if (type == 1) {

			refundSettleAmount = settleAmount
					.multiply(
							refundedProductAmount.divide(
									distributorProductAmount, 6,
									BigDecimal.ROUND_DOWN))
					.setScale(2, BigDecimal.ROUND_HALF_UP).negate();
		} else {

			refundSettleAmount = settleAmount
					.multiply(
							new BigDecimal(refundProductDetail.getRefund_num()))
					.divide(new BigDecimal(refundProductDetail.getProduct_num()),
							2, BigDecimal.ROUND_DOWN).negate();
		}

		return refundSettleAmount;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_001() {
		Logger.start(true, "代购神器，没有设置分销比例，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, false,
							false);

			SqDistributorService.buildSqDistributorRelation(
					productBean.getSellerId(), 0, 3, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithSalesman(productBean, 0L);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_002() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1-》一级分销员2，未结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain(
							(int) ShenqiUserSet.dualPayModeGoodsOwnerId, 1,
							false, false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(productBean,
							userId, 0L, productBean.getGoodsOwnerId());

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_003() {
		Logger.start(true, "代购神器， 分销员是顶级商户，不参与分销，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			SqDistributorService.setDistributorConfig(merchantId, 1, false);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, merchantId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, 0L);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			this.verify(paymentId, new HashMap<Long, BigDecimal>(), true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_004() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							2, 0);
			SqDistributorService.setDistributorConfig(merchantId, 1, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_005() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_006() {
		Logger.start(true, "代购神器，分销员体系，盛付通支付"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3-》一级分销员4，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							5, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(4));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

//			ShenqiSettlementService.doSettlement();
//			Thread.sleep(3000);
//
//			// 代购神器分销订单结算
//			SqDistributorService.doDistributorSettlement();
//			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_007() {
		Logger.start(true, "代购神器，分销员体系，外币支付"
				+ "商户-》一级分销员1-》二级分销员1-》三级分销员1，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							true);
			String currency = productBean.getCurrency();

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(1));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndCurrency(productBean, userId,
							currency);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_008() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》二级分销员1-》二级分销员2-二级分销员3，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 1,
							3, firstLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, secondLevelList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_009() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》三级分销员1-》三级分销员2-》三级分销员3，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							1, 0);
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 2,
							3, firstLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_010() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》二级分销员1-》三级分销员1-》三级分销员2，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(1));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 2,
							secondLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, 0L);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_011() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1-》一级分销员2-》二级分销员1，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(2));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, secondLevelList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_012() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1-》二级分销员1-》二级分销员2，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(1));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, secondLevelList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_013() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》二级分销员1-》二级分销员2-》三级分销员1，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_014() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》二级分销员1-》三级分销员1-》三级分销员2，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();

//			long merchantId = 500041475L;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 2,
							secondLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_015() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》二级分销员1-》三级分销员1-》三级分销员2-》三级分销员3，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 3,
							secondLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_016() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》二级分销员1-》二级分销员2（清退）-》三级分销员1-》三级分销员2（不允许自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.setDistributorConfig((long) merchantId, 3,
					false);
			SqDistributorService
					.updateDistributorStatus(secondLevelList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_017() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》二级分销员1-》二级分销员2-》三级分销员1-》三级分销员2（允许自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.setDistributorConfig((long) merchantId, 3,
					true);

			// 商户开通分销机构和CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			// Long userId = placeOrderResp.getUserId();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_018() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》二级分销员1-》二级分销员2-》三级分销员1-》三级分销员2（允许自购，清退），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));

			SqDistributorService.setDistributorConfig(merchantId, 3, true);
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.updateDistributorStatus(distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_019() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1（不允许自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);

			SqDistributorService.setDistributorConfig(merchantId, 1, false);
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_020() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2，删除分销员与消费者绑定关系，未结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));
			SqDistributorService.updateConsumerStatus(merchantId, userId, true);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			this.verify(paymentId, new HashMap<Long, BigDecimal>(), true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_021() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》二级分销员1-》二级分销员2-》三级分销员1-》三级分销员2（允许自购，清退），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));

			SqDistributorService.setDistributorConfig(merchantId, 3, true);
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.updateDistributorStatus(distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_022() {
		Logger.start(true, "代购神器，分销体系，渠道商负利润订单，已接单，已结算，渠道商没有余额扣除，无法结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderNegativeProfitWithUserId(productBean,
							BigDecimal.ZERO, autotest_goodsOwnerId,
							productBean.getCurrency(), userId, false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_023() {
		Logger.start(true, "代购神器，分销体系，分销员的分销金额为0，已接单，已结算，渠道商没有余额扣除，无法结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderNegativeProfitWithUserId(productBean,
							BigDecimal.ZERO, autotest_goodsOwnerId,
							productBean.getCurrency(), userId, true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_024() {
		Logger.start(true,
				"代购神器外币标价商品，商品标价与货头结算币种不一致, 直销订单并支付, 分销订单，hipopay渠道, 未调价，已接单，已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain(
							(int) ShenqiUserSet.multiCurrencyGoodsOwnerId, 0,
							false, "JPY");

			String currency = productBean.getCurrency();

			productBean = DGSQBuildProxyChainCallService.createChannel(
					productBean.getSellerId(), productBean.getProductId(),
					"JPN");

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndGoodsOwnerIdAndCurrency(
							productBean, userId,
							ShenqiUserSet.multiCurrencyGoodsOwnerId, currency);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer");

			// 根据人民币离岸汇率计算
			BigDecimal exchangeRmbRate = DGSQBaseService.getCurrencyConfig(
					currency).getExchangeRmbRate();

			SQPayGateWayCallService
					.DGSQNotifyCall(
							String.valueOf(thirdTradingId),
							totalPrice.subtract(BigDecimal.ZERO)
									.multiply(exchangeRmbRate)
									.setScale(2, BigDecimal.ROUND_HALF_DOWN)
									.toString(), userId.intValue(),
							BigDecimal.ZERO.toPlainString(), "CNY", "USD");

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(
					ShenqiUserSet.multiCurrencyGoodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(
					ShenqiUserSet.multiCurrencyGoodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_025() {
		Logger.start(true, "代购神器，多货头，多币种下单，分销订单，结算币种为日币，未调价，已接单，已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithSuppliersAndDistributor(
							autotest_goodsOwnerId, 1,
							ShenqiUserSet.japanGoodsOwnerId, 5, 1);

			long orderId1 = placeOrderResp.getOrderList().get(0).getOrderId();
			long orderId2 = placeOrderResp.getOrderList().get(1).getOrderId();

			Long userId = placeOrderResp.getUserId();

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId1, BigDecimal.ZERO);
			map.put(orderId2, BigDecimal.ZERO);

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId1);
			SqTradingMqService.updateLiveOrder(false, orderId2);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId1);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId1)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId1);
			TradingSyncService.getSyncResult(orderId1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(
					ShenqiUserSet.japanGoodsOwnerId, orderId2);
			TradingSyncService.getSyncResult(orderId2);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId1,
					null, null);
			TradingSyncService.getSyncResult(orderId1);

			// 发货
			SqTradingCallService.delivery(ShenqiUserSet.japanGoodsOwnerId,
					orderId2, null, null);
			TradingSyncService.getSyncResult(orderId2);

			// 代购神器结算
			ShenqiSettlementService.doSettlement();

			Thread.sleep(5000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + k));

			this.verify(paymentId, map, true, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_026() {
		Logger.start(true,
				"代购神器人民币标价商品，商户不允许收人民币，payChannelType为1，直销订单并支付, 分销订单，hipopay渠道, 未调价，已接单，已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain(500956988, 0, false);
			String currency = "USD";

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndGoodsOwnerIdAndCurrency(
							productBean, userId, 500956988L, "CNY");

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer");

			// 根据人民币离岸汇率计算
			BigDecimal exchangeRmbRate = DGSQBaseService.getCurrencyConfig(
					"USD").getExchangeRmbRate();

			totalPrice = totalPrice.multiply(
					BigDecimal.ONE.divide(exchangeRmbRate, 7,
							BigDecimal.ROUND_HALF_DOWN)).setScale(2,
					BigDecimal.ROUND_UP);

			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					currency, currency);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(500956988, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(500956988, orderId, null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_027() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，未结算，部分退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			int productCount = 2;

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId, productCount);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_028() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，未结算，全部退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_029() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，已结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_030() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，各个商品依次退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			for (int i = 0; i < placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().size(); i++) {

				OrderDetailPo orderDetailPo = placeOrderResp.getOrderList()
						.get(0).getOrderDetailPoList().get(i);

				RefundService.createOnePartProductRefundSuccess(orderId,
						orderDetailPo, autotest_goodsOwnerId, paymentId,
						userId, orderDetailPo.getProductCount());
			}

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_031() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，部分退货退款，验证分销数据，已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			int productCount = 2;

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId, productCount);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			// 代购神器结算
			ShenqiSettlementService.doSettlement();

			Thread.sleep(5000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_033() {
		Logger.start(
				true,
				"代购神器，分销员体系，"
						+ "商户-》一级分销员1-》一级分销员2-》一级分销员3-》一级分销员4（既是分销员，又绑定了消费者关系，不允许自购），已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			String mobile = TestDataManager.getPhoneNum();

			Long userId = SqDistributorService.createMemberUser(merchantId);
			Long distributorId = SqDistributorService.createDUser(merchantId,
					mobile);

			// 创建分销链并配置商户
			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);

			// 添加一个分销员
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(3), distributorId, 0);

			// 绑定消费者
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 绑定会员
			SqDistributorService.bindMobile(merchantId, userId, mobile, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_034() {
		Logger.start(
				true,
				"代购神器，分销员体系，"
						+ "商户-》一级分销员1-》一级分销员2-》一级分销员3-》一级分销员4（既是分销员，又绑定了消费者关系，允许自购），已结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			String mobile = TestDataManager.getPhoneNum();

			Long userId = SqDistributorService.createMemberUser(merchantId);
			Long distributorId = SqDistributorService.createDUser(merchantId,
					mobile);

			// 创建分销链并配置商户
			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, true);

			// 商户开通分销机构和CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 添加一个分销员
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(3), distributorId, 0);

			// 绑定消费者
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 绑定会员
			SqDistributorService.bindMobile(merchantId, userId, mobile, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	// 分销小店店主没有足够的钱分佣，无法结算
	public void Tc_ShenqiDistributorSettlement_035() {
		Logger.start(true, "代购神器，分销小店+分销员体系" + "商户-》一级分销员1，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Integer mchId = DGSQBaseService.createIndependenceMch("USA");
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createRetailShopProxychain(mchId, BigDecimal.TEN, false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							2, 0);
			SqDistributorService.setDistributorConfig(merchantId, 1, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndGoodsOwnerIdAndCurrency(
							productBean, userId, (long) mchId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(mchId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(mchId, orderId, null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_036() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，其中一个商品部分退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			OrderDetailPo orderDetail = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetail, autotest_goodsOwnerId, paymentId, userId, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_037() {
		Logger.start(true, "代购神器，分销体系，分销员的分销金额为0，申请退货退款，渠道商没有余额扣除，无法结算");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderNegativeProfitWithUserId(productBean,
							BigDecimal.ZERO, autotest_goodsOwnerId,
							productBean.getCurrency(), userId, true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(10000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetail = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetail, autotest_goodsOwnerId, paymentId, userId, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_038() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_039() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	// 为结算的分销员订单，可能因为整单退款而取消结算，导致没有退款结算单
	public void Tc_ShenqiDistributorSettlement_040() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，未结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndMultiProsInOneOrder(productBean,
							userId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_041() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，未结算，全部退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, 1234L);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			int productCount = orderDetailPoList.get(0).getProductCount();

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId, productCount);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_042() {
		Logger.start(true, "代购神器，分销员体系，" + "商户-》二级分销员1，用户下单时绑定了客服，不进行分销");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, true);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, firstLevelList.get(0), distributorId, 0);

			SqDistributorService.setDistributorConfig((long) merchantId, 3,
					true);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger
					.comment("placeOrderProfit: " + v + ", orderId: " + k
							+ ", paymentId: " + paymentId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_043() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 2);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 10);
			// 商户开通分销机构
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_044() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_045() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，未结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_046() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，未结算，全部退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			int productCount = orderDetailPoList.get(0).getProductCount();

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId, productCount);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_047() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，线下结算，用户未点击CPS链接，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 商户开通CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 10);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_048() {
		// 当期时间为12月时，测试一下
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，周期结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					1, 31);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_049() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，线下结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			// AgentProductBean productBean = DGSQBuildProxyChainCallService
			// .createProxychain((int) 500011795L, 0, false, false);
			long merchantId = productBean.getSellerId();

			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);
			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// Long CPSDistributorId = 501565934L;
			// Long distributorId = 501571119L;

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 1,
					0, 10);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			Thread.sleep(1000);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_050() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，周期结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);
			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					1, 15);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_051() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，分销员与用户绑定过期，没有分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 设置分销员与用户绑定的有效期为5天
			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1)
					.withUserDistributorBindRuleType(3)
					.withBindingEffectiveThreshold(5);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0, distributorLevelCommissionConfigVo);

			// 商户开通CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 修改用户与分销员的过期时间
			distributorConsumerRelationWrapper.updateExpirationDate(userId,
					distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_052() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，用户点击时间过期，没有分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);
			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 1,
					0, 0);

			// 商户开通分销机构配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 修改点击事件为30天前
			userCpsEventWrapper.updateAddTimeByUserIdAndDistributorId(userId,
					distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_053() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1，分销员与用户绑定时间过期，没有分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							2, 0);

			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorConfig(merchantId, 1, false, 3,
					5);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 修改用户与分销员的过期时间
			distributorConsumerRelationWrapper.updateExpirationDate(userId,
					distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_054() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，部分退货退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			RefundService.createPartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_055() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员（自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, 0, merchantId, 0);

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 1001);

			Long agencyDistributorId = distributorList.get(0);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_056() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构（自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, 0, merchantId, 0);
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, merchantId, distributorId, 1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_057() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员（自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 2);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 2001);

			Long agencyDistributorId = distributorList.get(0);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 2);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_058() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS（自购），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, merchantId, distributorId, 2);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_059() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员（没有配置信息），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 1001);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// // 设置分销机构子分销员配置
			// SqDistributorService.setSubDistributorAgencyConfig(merchantId,
			// agencyDistributorId, 1);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_060() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员（没有配置信息），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 2);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 2001);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_061() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，未结算，取消订单，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);
			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 1,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			SqTradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.SELLER_CANCEL_ORDER,
					autotest_goodsOwnerId);

			Thread.sleep(3000);

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_062() {
		Logger.start(true, "代购神器，CPS体系"
				+ "商户-》CPS->分销员，多条点击行为，取最早的点击行为，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);
			Thread.sleep(5000);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					501603189L, 25919728L);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_063() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2，连锁店模式，借用主店分销员，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// List<Long> distributorList = SqDistributorService
			// .buildSqDistributorRelation(autotest_goodsOwnerId, 0, 2, 0);
			// SqDistributorService.setDistributorConfig(merchantId, 1, false);
			// SqDistributorService.bindDistributorConsumerRelation(userId,
			// merchantId, distributorList.get(1));

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_064() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，连锁店模式，借用主店分销机构，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							autotest_goodsOwnerId, 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 1,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeChainStoreOrderWithUserId(chainStoreProductBean,
							chainStoreGoodsOwnerId,
							chainStoreProductBean.getCurrency(), userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_065() {
		Logger.start(true, "代购神器，CPS体系"
				+ "商户-》CPS->分销员（没有配置信息），连锁店模式，借用主店CPS，线下周期结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser(autotest_goodsOwnerId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(autotest_goodsOwnerId, 0,
							1, autotest_goodsOwnerId, 2);

			SqDistributorService.buildSingleSqDistributorRelation(
					autotest_goodsOwnerId, 0, distributorList.get(0),
					distributorId, 2001);

			SqDistributorService.setDistributorAgencyConfig(
					autotest_goodsOwnerId, 2, 1, 1, 10);

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_066() {
		Logger.start(true, "代购神器，CPS体系"
				+ "商户-》CPS->分销员，未结算，整单退货退款，连锁店模式，借用主店CPS，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							autotest_goodsOwnerId, 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 1,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					chainStoreGoodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_067() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，连锁店模式，借用主店分销机构，部分退货退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							autotest_goodsOwnerId, 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 1,
					0, 10);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), chainStoreGoodsOwnerId,
					paymentId, userId, 1);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_068() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1，连锁店模式，使用微信支付，借用主店分销员，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(autotest_goodsOwnerId, 0, 2, 0);
			SqDistributorService.setDistributorConfig(merchantId, 1, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(1));

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_070() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，连锁店模式，使用微信支付，借用主店分销机构，部分退货退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			long merchantId = autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							autotest_goodsOwnerId, 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 10);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 使用子店商品下单
			AgentProductBean chainStoreProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(
							chainStoreProductBean, userId, null,
							chainStoreGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(chainStoreGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), chainStoreGoodsOwnerId,
					paymentId, userId, 1);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_071() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，仅退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, BigDecimal.TEN);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_072() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ShenqiDistributorSettlement_073() {
		Logger.start(true, "代购神器，CPS体系" + "商户-》CPS->分销员，未结算，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 2);

			Long CPSDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置CPS子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					CPSDistributorId, 2);

			// 保存用户点击CPS链接行为
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 退货退款
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, BigDecimal.TEN);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_074() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，先部分退货退款，再次仅退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退货退款成功的退款单
			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPo, autotest_goodsOwnerId, paymentId, userId, 1);
			Thread.sleep(3000);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"100.00"));

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_075() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，先仅退款，再次部分退货退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"100.00"));

			// 创建一个部分退货退款成功的退款单
			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPo, autotest_goodsOwnerId, paymentId, userId, 1);
			Thread.sleep(3000);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_076() {
		Logger.start(true, "代购神器，分销员+合伙人体系"
				+ "商户-》一级分销员1（合伙人1）-》一级分销员2（合伙人2），未结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain(
							(int) ShenqiUserSet.dualPayModeGoodsOwnerId, 1,
							false, false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(productBean,
							userId, 0L, productBean.getGoodsOwnerId());

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_077() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2-》一级分销员3（合伙人），已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_078() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1-》一级分销员2（合伙人），已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency(), 6010);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_079() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_080() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人体系"
						+ "商户-》二级分销员1（合伙人）-》二级分销员2（清退）（合伙人）-》三级分销员1-》三级分销员2（不允许自购）（合伙人），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));
			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.setDistributorConfig((long) merchantId, 3,
					false);
			SqDistributorService
					.updateDistributorStatus(secondLevelList.get(1));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorId);
			SqPartnerService.enablePartner(merchantId, secondLevelList.get(1));
			SqPartnerService.enablePartner(merchantId, secondLevelList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_081() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人体系"
						+ "商户-》二级分销员1（合伙人）-》二级分销员2（合伙人禁用）-》三级分销员1（合伙人）-》三级分销员2（允许自购）（合伙人），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 1, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 2,
							firstLevelList.get(0));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(1));

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					2, thirdLevelList.get(0), distributorId, 0);

			SqDistributorService.setDistributorConfig((long) merchantId, 3,
					true);

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorId);
			SqPartnerService.enablePartner(merchantId, thirdLevelList.get(0));
			SqPartnerService.enablePartner(merchantId, secondLevelList.get(1));
			SqPartnerService.disablePartner(merchantId, secondLevelList.get(1));
			SqPartnerService.enablePartner(merchantId, secondLevelList.get(0));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			// Long userId = placeOrderResp.getUserId();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_082() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人"
						+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），盛付通支付，已结算，部分退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency(), 6010);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, shengPayRate, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			int productCount = 1;

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId, productCount);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

//			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_083() {
		Logger.start(true, "代购神器，分销员体系+合伙人"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2-》一级分销员3，未结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_084() {
		Logger.start(true, "代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2（合伙人）-》一级分销员3，各个商品依次退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			for (int i = 0; i < placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().size(); i++) {

				OrderDetailPo orderDetailPo = placeOrderResp.getOrderList()
						.get(0).getOrderDetailPoList().get(i);

				RefundService.createOnePartProductRefundSuccess(orderId,
						orderDetailPo, autotest_goodsOwnerId, paymentId,
						userId, orderDetailPo.getProductCount());
			}

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_085() {
		Logger.start(true, "代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_086() {
		Logger.start(true, "代购神器，分销员体系+合伙人"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2-》一级分销员3，分销员佣金为0，未结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false, 1,
					null, true);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_087() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人"
						+ "商户-》一级分销员1（合伙人）-》一级分销员2-》一级分销员3，分销员佣金为0，合伙人佣金为0，未结算，整单退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false, 1,
					null, true);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId, true);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			RefundService.createFullRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_088() {
		Logger.start(true, "代购神器，分销员体系" + "商户-》一级分销员1-》一级分销员2，使用积分，未结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			// 设置积分
			GodPointsService.saveCreditPolicyReq(merchantId);
			Thread.sleep(500);

			// 赠送积分
			GodPointsService.sendCredit(merchantId, userId);
			Thread.sleep(500);

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerIdAndIntegral(
							productBean, userId, 0L,
							productBean.getGoodsOwnerId(), 20L);

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_089() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，使用积分，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			// 设置积分
			GodPointsService.saveCreditPolicyReq(merchantId);
			Thread.sleep(500);

			// 赠送积分
			GodPointsService.sendCredit(merchantId, userId);
			Thread.sleep(500);

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 商户开通CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerIdAndIntegral(
							productBean, userId, null,
							productBean.getGoodsOwnerId(), 20L);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_090() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2，使用积分，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			// 设置积分
			GodPointsService.saveCreditPolicyReq(merchantId);
			Thread.sleep(500);

			// 赠送积分
			GodPointsService.sendCredit(merchantId, userId);
			Thread.sleep(500);

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerIdAndIntegral(
							productBean, userId, null,
							productBean.getGoodsOwnerId(), 20L);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_091() {
		Logger.start(true, "代购神器，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，设置分销员指定商品分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 3, false,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_092() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
						+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），设置分销员和合伙人指定商品分佣，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 3, true,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_093() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2，设置分销员和合伙人指定商品分佣，费率为0，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 3, true,
					true);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_094() {
		Logger.start(true, "代购神器，分销员体系+合伙人体系"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2，设置合伙人指定商品分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), false, 3, true,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			TradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_095() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人，多个商品一个订单，资金不归集"
						+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），某个商品设置分销员和合伙人指定商品分佣，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) fundNotCollectGoodsOwnerId, 0,
							false, false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 3, true,
					false);

			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) fundNotCollectGoodsOwnerId, 0,
							false, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAgentProductListAndGoodsOwnerId(
							Arrays.asList(productBean, productBean2), userId,
							fundNotCollectGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_096() {
		Logger.start(true, "代购神器，分销员体系，外币支付"
				+ "商户-》一级分销员1-》二级分销员1-》三级分销员1，指定商品费率和级别分佣，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							true);
			String currency = productBean.getCurrency();

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> firstLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);
			List<Long> secondLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 1, 1,
							firstLevelList.get(1));
			List<Long> thirdLevelList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 2, 1,
							secondLevelList.get(0));

			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, thirdLevelList.get(0));

			SqDistributorService.setDistributorSettlementConfig(merchantId, 2);

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 1, false,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndCurrency(productBean, userId,
							currency);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_097() {
		Logger.start(true, "代购神器，分销机构体系，资金不归集"
				+ "商户-》分销员机构->分销员，指定商品分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) fundNotCollectGoodsOwnerId, 0,
							false, false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 商户开通CPS配置
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 1, false,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerId(productBean,
							userId, null, fundNotCollectGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(fundNotCollectGoodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(fundNotCollectGoodsOwnerId,
					orderId, null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_098() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员（已清除），已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.updateDistributorStatus(distributorId);

			// 分销机构
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_099() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员（自购且清除），验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			Long merchantId = productBean.getSellerId();
			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser((long) merchantId, false);

			Long userId = ((Integer) map.get("userId")).longValue();
			Long distributorId = ((Integer) map.get("distributorId"))
					.longValue();

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, 0, merchantId, 0);

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);

			SqDistributorService.buildSingleSqDistributorRelation(merchantId,
					0, distributorList.get(0), distributorId, 1001);

			Long agencyDistributorId = distributorList.get(0);

			SqDistributorService.updateDistributorStatus(distributorId);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 2, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> distributorMap = new HashMap<>();

			distributorMap.put(orderId, BigDecimal.ZERO);

			distributorMap.forEach((k, v) -> Logger.comment("paymentId: "
					+ paymentId + ", orderId: " + orderId));

			this.verify(paymentId, distributorMap, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_100() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构，指定类目分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(0));

			// 设置商品类目分佣
			SqDistributorService.saveAgencyCategoryCommissionRate(merchantId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_101() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，指定类目分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			// 设置商品类目分佣
			SqDistributorService.saveAgencyCategoryCommissionRate(merchantId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_102() {
		Logger.start(
				true,
				"代购神器，分销机构体系，多个商品一个订单，资金不归集" + "商户-》分销员机构，指定类目分佣，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) fundNotCollectGoodsOwnerId, 0,
							false, false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 1,
							productBean.getSellerId(), 1);
			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(0));

			// 设置商品类目分佣
			SqDistributorService.saveAgencyCategoryCommissionRate(merchantId);

			AgentProductBean productBean2 = DGSQBuildProxyChainCallService
					.createProxychain((int) fundNotCollectGoodsOwnerId, 0,
							false, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAgentProductListAndGoodsOwnerId(
							Arrays.asList(productBean, productBean2), userId,
							fundNotCollectGoodsOwnerId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_103() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，已结算，其中一个商品部分退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			OrderDetailPo orderDetail = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetail, autotest_goodsOwnerId, paymentId, userId, 1);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_104() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，已结算，全部退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, 1234L);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			RefundService.createPartProductRefund(orderId,
					orderDetailPoList.get(0), merchantId, userId);

			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_105() {
		Logger.start(true, "代购神器，分销机构体系" + "商户-》分销员机构->分销员，线下结算，已结算，部分退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 1,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			// 退货退款
			// 第一个订单下的商品列表
			List<OrderDetailPo> orderDetailPoList = order
					.getOrderDetailPoList();

			RefundService.createPartProductRefundSuccess(orderId,
					orderDetailPoList.get(0), autotest_goodsOwnerId, paymentId,
					userId);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_106() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，已结算，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_107() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，先部分退货退款，已结算，再次仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退货退款成功的退款单
						RefundService.createOnePartProductRefundSuccess(orderId,
								orderDetailPo, autotest_goodsOwnerId, paymentId, userId, 1);
						Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"100.00"));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_108() {
		Logger.start(true, "代购神器，分销机构体系"
				+ "商户-》分销员机构->分销员，先仅退款，已结算，再次部分退货退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"100.00"));

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			// 创建一个部分退货退款成功的退款单
			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPo, autotest_goodsOwnerId, paymentId, userId, 1);
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_109() {
		Logger.start(true, "代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1（合伙人）-》一级分销员2（合伙人）-》一级分销员3，已结算，各个商品依次退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			for (int i = 0; i < placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().size(); i++) {

				OrderDetailPo orderDetailPo = placeOrderResp.getOrderList()
						.get(0).getOrderDetailPoList().get(i);

				RefundService.createOnePartProductRefundSuccess(orderId,
						orderDetailPo, autotest_goodsOwnerId, paymentId,
						userId, orderDetailPo.getProductCount());
			}

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_110() {
		Logger.start(true, "代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
				+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），已结算，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_111() {
		Logger.start(
				true,
				"代购神器，分销员体系+合伙人，多个商品一个订单，包含优惠和折扣"
						+ "商户-》一级分销员1-》一级分销员2（合伙人）-》一级分销员3（合伙人），设置分销员和合伙人指定商品分佣，已结算，仅退款，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));
			SqPartnerService.enablePartner(merchantId, distributorList.get(3));

			// 设置指定商品分佣
			SqDistributorService.saveProductCommission(merchantId,
					Arrays.asList(productBean.getProductId()), true, 3, true,
					false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, RefundService
							.getOrderMaxRefundAmount(placeOrderResp
									.getOrderList().get(0)));

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("orderId: " + k
					+ ", paymentId: " + paymentId));

			this.verify(paymentId, map, true, true, false, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_112() {
		Logger.start(true, "代购神器，分销员体系，多个商品一个订单，包含优惠和折扣"
				+ "商户-》分销员机构->分销员，先部分退货退款，再次仅退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProduct(productBean, userId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退货退款成功的退款单
			RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPo, autotest_goodsOwnerId, paymentId, userId, 1);
			Thread.sleep(3000);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"100.00"));

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, false, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_113() {
		Logger.start(true, "中免澳门大会员，分销机构体系" + "商户-》分销员机构->分销员，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) macaoCDFGoodsOwnerId, 0, false, true);

			Long userId = 502603484L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeCDFMacaoOrderIntegral(productBean, 10L);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "502665591", "7200");

			SqTradingCallService.orderPayCallBack(thirdTradingId, userId,
					totalPrice);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(macaoCDFGoodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(macaoCDFGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true, true, 0);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_114() {
		Logger.start(true, "中免澳门大会员，分销机构体系" + "商户-》分销员机构->分销员，整单退款，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) macaoCDFGoodsOwnerId, 0, false, true);

			Long userId = 502603484L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeCDFMacaoOrderIntegral(productBean, 10L);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "502665591", "7200");

			SqTradingCallService.orderPayCallBack(thirdTradingId, userId,
					totalPrice);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(macaoCDFGoodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(macaoCDFGoodsOwnerId, orderId, null,
					null);
			TradingSyncService.getSyncResult(orderId);

			RefundService.createFullRefundSuccess(orderId,
					macaoCDFGoodsOwnerId, paymentId, userId);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			this.verify(paymentId, map, true, true, true, 1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_115() {
		Logger.start(true, "中免澳门大会员，分销员+合伙人体系"
				+ "商户-》一级分销员1（合伙人1）-》一级分销员2（合伙人2），未结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) macaoCDFGoodsOwnerId, 0, false, true);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeCDFMacaoOrderIntegralWithUserId(productBean, userId,
							0L);

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "502665591", "7200");

			SqTradingCallService.orderPayCallBack(thirdTradingId, userId,
					totalPrice);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_116() {
		Logger.start(true, "代购神器，分销员+合伙人体系"
				+ "商户-》一级分销员1（合伙人1）-》一级分销员2（合伙人2），内购活动订单，不生成结算单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain(
							(int) ShenqiUserSet.dualPayModeGoodsOwnerId, 1,
							false, false);

			Long userId = (long) new OrderCallService().createNewUser();
			Long merchantId = productBean.getSellerId();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 3, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(2));

			// 设置合伙人配置
			SqPartnerService.savePartnerConfig(merchantId);

			// 将分销员设置成合伙人
			SqPartnerService.enablePartner(merchantId, distributorList.get(1));
			SqPartnerService.enablePartner(merchantId, distributorList.get(2));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderInnerSaleActivity(productBean,
							userId, productBean.getGoodsOwnerId());

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_117() {
		Logger.start(true, "中免大会员，分销机构体系" + "商户-》分销员机构->分销员，招商跨境支付，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 0,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCMBPayAndCrossBoderType(productBean, 1, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "500024716", "8000");

			SqTradingCallService.orderPayCMBCallBack(thirdTradingId, userId,
					totalPrice);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_118() {
		Logger.start(true, "中免大会员，分销机构体系" + "商户-》分销员机构->分销员，招商国内支付，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							productBean.getSellerId(), 1);

			Long agencyDistributorId = distributorList.get(0);
			Long distributorId = distributorList.get(1);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 1,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorId);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCMBPayAndCrossBoderType(productBean, 2, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "500024716", "8000");

			SqTradingCallService.orderPayCMBCallBack(thirdTradingId, userId,
					totalPrice);

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ShenqiDistributorSettlement_119() {
		Logger.start(true, "代购神器，平台供货商，分销员体系"
				+ "商户-》一级分销员1-》一级分销员2-》一级分销员3，已结算，验证分销数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createPlatformProxychain(
							(int) platformSupplierGoodsOwnerId, 1, false, "CNY");

			long merchantId = productBean.getSellerId();
			Long userId = (long) new OrderCallService().createNewUser();

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(productBean.getSellerId(), 0,
							4, 0);
			SqDistributorService.setDistributorConfig(merchantId, 3, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(3));

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSaleman(productBean, userId, null);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			long paymentId = wapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = wapper.selectPaymentById(paymentId);

			this.onHpSettled(paymentId, payment, null, 1);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(platformSupplierGoodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 发货
			SqTradingCallService.delivery(platformSupplierGoodsOwnerId, orderId,
					null, null);
			TradingSyncService.getSyncResult(orderId);

			ShenqiSettlementService.doSettlement();
			Thread.sleep(3000);

			// 代购神器分销订单结算
			SqDistributorService.doDistributorSettlement();
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			HashMap<Long, BigDecimal> map = new HashMap<>();

			map.put(orderId, BigDecimal.ZERO);

			map.forEach((k, v) -> Logger.comment("paymentId: " + paymentId
					+ ", orderId: " + orderId));

			this.verify(paymentId, map, true, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void tetRefund() {

		Ts_ShenqiDistributorSettlement test = new Ts_ShenqiDistributorSettlement();

		test.Tc_ShenqiDistributorSettlement_027();
//		test.Tc_ShenqiDistributorSettlement_028();
//		test.Tc_ShenqiDistributorSettlement_029();
//		test.Tc_ShenqiDistributorSettlement_030();
//		test.Tc_ShenqiDistributorSettlement_031();
//		test.Tc_ShenqiDistributorSettlement_036();
//		test.Tc_ShenqiDistributorSettlement_040();
//		test.Tc_ShenqiDistributorSettlement_041();
//		test.Tc_ShenqiDistributorSettlement_054();
//		test.Tc_ShenqiDistributorSettlement_071();
//		test.Tc_ShenqiDistributorSettlement_072();
//		test.Tc_ShenqiDistributorSettlement_074();
//		test.Tc_ShenqiDistributorSettlement_075();
//		test.Tc_ShenqiDistributorSettlement_082();
//		test.Tc_ShenqiDistributorSettlement_083();
//		test.Tc_ShenqiDistributorSettlement_084();
//		test.Tc_ShenqiDistributorSettlement_085();
//		test.Tc_ShenqiDistributorSettlement_086();
//		test.Tc_ShenqiDistributorSettlement_087();
//		test.Tc_ShenqiDistributorSettlement_092();
//		test.Tc_ShenqiDistributorSettlement_103();
//		test.Tc_ShenqiDistributorSettlement_104();
//		test.Tc_ShenqiDistributorSettlement_105();
//		test.Tc_ShenqiDistributorSettlement_106();
//		test.Tc_ShenqiDistributorSettlement_107();
//		test.Tc_ShenqiDistributorSettlement_108();
//		test.Tc_ShenqiDistributorSettlement_109();
//		test.Tc_ShenqiDistributorSettlement_110();
//		test.Tc_ShenqiDistributorSettlement_111();
//		test.Tc_ShenqiDistributorSettlement_112();
	}

	public static void main(String[] args) throws Exception {

		Ts_ShenqiDistributorSettlement test = new Ts_ShenqiDistributorSettlement();

		Long orderId = Long.valueOf("14333920441");
		Long paymentId = Long.valueOf("21423990441");

		hpSettledReq = new OnHpSettledReq();

		hpSettledReq.setTradingId(paymentId);
		hpSettledReq.setSettleFee(null);

		HashMap<Long, BigDecimal> map = new HashMap<>();

		map.put(orderId, BigDecimal.ZERO);

		test.verify(paymentId, map, true, true, false, 1);
	}
}
